Dec 3

Selenium RCを使ってrhacoでUnitTestするためのライブラリを作りました

rhacoはViewsとかWebアプリを作るための非常に便利なクラスがあるのですが、この部分ではrhacoのテスト機能を使えないというのが気になってました。

先日のPHP関西勉強会でSelenium RCの話題が出て、これならrhacoのテストでも使えるのではないかと思い、pearのTesting::Seleniumを改変してrhaco用に使えるようにしました。

注:rhacoは2008/12/2以降のver 1.6.1 trunkを使用して下さい。でないと下のコードは動きません。

arboのutil.SeleniumTestにアップしております。

使用例(OS:Windows XP, Webサーバ:Apache2):

  • Selenium RCの起動

  • SeleniumのサイトからSelenium RCをダウンロードし、解凍したファイルに含まれる「selenium-server.jar」を起動します。

現在のバージョンは1.0bですが、これではfirefox3やie7で正常に動きません。nightly-buildを使えば動きます

java -jar selenium-server.jar

  • セットアップ

  • 丸ごとrhacoにあるようにセットアップします。セットアップでは設定およびテーブルの生成を行います。例としてproject.xmlは次のものを用います。

project.xml
<project rhacover="1.6.1" version="0.0.1" name="seleniumrhaco" xmlns="http://rhaco.org" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://rhaco.org http://m.rhaco.org/xsd/project_1_6_1.xsd">
<database name="hoge">
	<table name="table1">
		<column name="id" type="serial"></column>
		<column name="name" type="text"></column>
	</table>
</database>
</project>

  • SeleniumTestの配置

  • library/arbo/util/にSeleniumTest.phpを配置します。セットアップ画面のinstallからSeleniumTestを検索してinstallすると上記位置に配置されます。

  • コーディング

  • 以下のファイルを作ります。

index.php
require_once('./__init__.php');
Rhaco::import("generic.Urls");
Rhaco::import("database.DbUtil");
Rhaco::import("model.Table1");
$parser = Urls::parser(
array(
	'^$'=>array('method'=>'read','args'=>array(new Table1())),
	'^create$'=>array('method'=>'create','args'=>array(new Table1(),Rhaco::url('index.php')))
)
,new DbUtil(Table1::connection()));
$parser->write();

library/Hoge.php (テスト実行するためのダミーファイル)
class Hoge{
	function fuga(){
		/*** unit("HogeTest"); */
	}
}

setup/tests/HogeTest.php
Rhaco::import("arbo.util.SeleniumTest");
ini_set("max_execution_time","0");
class HogeTest extends SeleniumTest {
	function begin(){
		$this->setBrowserName("*firefox");//ieなら*iexplore,operaなら*opera,
		$this->setTargetUrl("http://localhost");
		$this->start();
	}
	function testCreate(){
		$this->open(Rhaco::url("index.php/create"));
		$this->waitForPageToLoad();
		$this->assertTrue($this->isTextPresent("name"));
		$this->type("name=name","ふが");
		$this->click("name=save_create");
		$this->waitForPageToLoad();
		$this->assertEquals($this->getLocation(),Rhaco::url('index.php'));
	}
	function finish(){
		$this->stop();
		$db = new DbUtil(Table1::connection());
		$db->delete(new Table1(),new C(Q::eq(Table1::columnName(),"ふが")));
	}
}

setBrowserで指定する書式はここが参考になります
  • テスト

  • セットアップ画面のtestページでlibrary/Hoge.phpを選んでexecuteボタンを押します。


以上でテストが実行されます。

※テストを行いたいブラウザのパスがデフォルトと違う場合はパスの設定が必要など対処が必要です。

Seleniumで使用できるコマンドはこちら

要素の選択方法(Element locator)についてはこちら

| comment(0)