GANCHIKU.com

Selnium.phpの使い方について

2006年6月22日
PHP

自分用のメモ。ちなみにまだ全部テストしていないので、動かないところがあるかも。テストツールなのに、テストしてないってヤヴァイな。テストをしたら、MLで議論して、頃合いを見て、Proposalにあげるかな。

つか、バージョンの管理ってどうしたらいいのかなー。どの時点で、0.1をリリースしたらいいのかわからん。つか、今のバージョンかな。

PHP4でのエラーの処理の場合は、return PEAR::raiseError(“hogehoge”);をするわけだが、なんかすべてのメソッドは、プライベートメソッドの_doCommandメソッドを呼び出すので、Errorオブジェクトを返さないといけないのかな。めんどくせー。ダメなものは、そこでPEAR_ERRRO_DIEで死ねばいいのに。。。その辺どうしたらいいかわからんかったけど、先人達に倣って、修正した。

ところで、Selenium.phpは、PHPUnitもしくは、Simpletestと相性がいい。と勝手に思っている。テストケースからよろしくブラウザを立ち上げてテストをしてくれるからなのだ。Simpletestにあって、PHPUnitになかったWebTestCaseなんて関係がなくなる。だって、SeleniumRCがよろしくブラウザをシミュレートしてくれるから。まぁ、好きな方を使えばいいのではないかな。私個人としては、PHPUnitを最初に使ったので、Simpletestはaskeetのチュートリアルでしか使ったことがない。なので、よくわからない。なんか同じような印象を持ったけども。

ちょっと簡単なサンプルを書いてみた。SeleniumRC Serverを動かしてないと使えないけども。。。ちなみにテストは失敗する。日本のロケールであれば。Searchでなくて、検索って変えられちゃうから。サンプルとしていけてないかなー。(手で大なり小なりをエスケープしたので、タイポがあるかも、上のソースは大丈夫だけど。)

  • Simpletestを使う場合
<?php
// To see this example, you need to have simpletest library.
require_once 'Selenium.php';
require_once 'simpletest/unit_tester.php';
require_once 'simpletest/reporter.php';

class SeleniumTest extends UnitTestCase
{
    function setUp()
    {
        $this->selenium =& new Testing_Selenium("*firefox", "http://www.google.com/");
        $result = $this->selenium->start();
        if (PEAR::isError($result)) {
            echo $result->getMessage();
            exit;
        }
    }
    function tearDown()
    {
        $this->selenium->stop();
    }

    function testGoogleSearch()
    {
        $this->selenium->open("http://www.google.com/webhp");
        $this->selenium->type("q", "hello world");
        $this->selenium->click("btnG");
        $this->assertEqual("hello world - Google Search", $this->selenium->getTitle());
    }
}
$test =& new SeleniumTest();
$test->run(new TextReporter());
?>
  • PHPUnitを使う場合
<?php
// To see this example, you need to have PHPUnit library.
require_once 'Selenium.php';
require_once 'PHPUnit.php';

class SeleniumTest extends PHPUnit_TestCase
{
    function SeleniumTest($name)
    {
        $this->PHPUnit_TestCase($name);
    }
    function setUp()
    {
        $this->selenium =& new Testing_Selenium("*firefox", "http://www.google.com/");
        $result = $this->selenium->start();
        if (PEAR::isError($result)) {
            echo $result->getMessage();
            exit(0);
        }
    }
    function tearDown()
    {
        $this->selenium->stop();
    }

    function testGoogleSearch()
    {
        $this->selenium->open("http://www.google.com/webhp");
        $this->selenium->type("q", "hello world");
        $this->selenium->click("btnG");
        $this->assertEquals("hello world - Google Search", $this->selenium->getTitle());
    }
}

$suite = new PHPUnit_TestSuite("SeleniumTest");
$result = PHPUnit::run($suite);
echo $result->toString();
?>l;

PHP5のE_STRICTの対応どうしようかなー。E_STRICTだと、たぶんHTTP_Requestでこけるので、そこは自分で作らないといけないのかな。Service_YahooとかもPHP5な書き方しているけど、HTTP_Requestを使っているので、アウトだと思うのだが。
うーん、MLに聞きたいのだけども、まだ登録されてないみたいだ。Pierreに早く登録してくれるように頼むかな。

しかし、PHPDocumentorが中途半端にドキュメントを作成してくれるのは困ったな。。。コメントの書き方がまずかったのだろう。

それと、ひっかかっていることとして、XSLTで書いてantで自動生成がいいかなーなんて思ってみたりするんだけど、どうなんかなー。なんか、SeleniumRCのバージョンがあがったときとかもよろしくそれに対応するコードを生成してくれるらしいんだけど、悩ましい。うーん。悩ましい。

つか、タグがエスケープされないんだった。独自でエスケープしないといけないじゃん。めんどくせー、MT。プラグインを入れたらいけるのかな。。。

コメントをどうぞ

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

*

次のHTML タグと属性が使えます: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Shin Ohno 2003-2012