Seleniumのメモ

Selenium概要
次のコンポーネントから構成されている。
 Selenium Core - Selenium本体
 Selenium IDE - Webブラウザから編集を可能にするFirefox拡張機能
 Selenium Server - リモートサーバ経由で試験を実施するためのコンポーネント

Selenium CoreはテストするWebアプリケーションと同じサーバで実行
Selenium Serverは、JavaScriptを使って画面遷移をテストすることができる
Selenium IDEでは手軽にテストをすることができる

PHPUnitSeleniumでテスト

PHPUnitSeleniumのテストパターンを書く事ができ、
Seleniumサーバーを通して実際にブラウザでのテストを実行、
その結果をPHPUnitで確認できる
[テストケース]→[PHPUnit]→[Selenium Server]→[ブラウザ]→[テストサーバ]

●準備
XAMPPがインストールされていること.
PHPUnitSelenium Serverをローカルマシンにインストールします。
 PHPUnitPEARを通してインストール.
チャネル

$ pear channel-discover pear.phpunit.de
 $ pear channel-discover pear.symfony-project.com
 $ pear install phpunit/PHPUnit

Selenium Serverの起動

 java -jar /[展開したディレクトリへのパス]/selenium-server-2.0b3/selenium-server.jar 

[Win+XAMPP]-----
参考:http://d.hatena.ne.jp/sakana_eie/20110403/1301778000

1. php.ini

[php.ini] を開き、下記の行のコメントアウトを外す。

extension=php_curl.dll


2. PHPUnit

コマンドプロンプトを管理者権限で起動し、下記を実行する。

cd C:\xampp\php
pear channel-update pear.php.net
pear upgrade pear
pear channel-discover components.ez.no
pear channel-discover pear.phpunit.de
pear channel-discover pear.symfony-project.com
pear install -a phpunit/PHPUnit
pear install -a Testing_Selenium-beta
phpunit --version

3. Selenium Server

http://seleniumhq.org/download/
にアクセスし、[Selenium Server] をダウンロードする。
ここでは [selenium-server-standalone-2.0b3.jar] を
[D:\xampp\selenium] に配置したものとする。

コマンドプロンプトを管理者権限で起動し、下記を実行する。

java -jar D:\hoge\selenium-server-standalone-2.0b3.jar


4. テストケース

[D:\selenium\selenium-test\]

テストケースを書く
参考:http://www.phpunit.de/manual/3.4/ja/selenium.html
 ポイントは以下の3点.
 1.PHPUnit/Extensions/SeleniumTestCase.phpをrequireしておく。
 2.テストケースの基底クラスにPHPUnit_Extensions_SeleniumTestCaseを指定する。
 3.テストを実行するブラザを指定する

./case/function/SampleTest.php

setBrowser('*safari');
$this->setBrowserUrl('http://www.google.co.jp/');
// $this->setSleep(3); //=> waitでの待ち時間を変更する。標準は1秒。
}

public function testTitle()
{
$this->open('http://localhost/');
$this->clickAndWait('link=テストリンク');
$this->assertTitle('テストサイト');
}

}

※要素を指定するElement Locatorは、次の方法がある
・リンクに含まれる文字列で指定(例:link=新規登録)
JavaScriptでDOMをたどって指定(例:document.forms[0].elements[0])
XPathで指定(例://input[@name='foo'])
 (例

テキスト
の場合、//div[@class='caution'])

※例:
//ドロップダウン string addSelection (string $locator, $optionLocator)
$this->select("searchDropdownBox", "label=本");

//インプット string type (string $locator, string $value)
$this->type("twotabsearchtextbox", "selenium");

//クリック string click (string $locator)
$this->click("//input[@type='image']");
//これはをクリック

//ページロードを待つ string waitForPageToLoad ([integer $timeout = NULL])
$this->waitForPageToLoad("30000");

//クリックして待つ string clickAndWait (string $locator)
$this->clickAndWait('link=Blog');

//タイトルが $title と異なる場合にエラーを報告します。
$this->assertTitle($title);

//$textが含まれていない場合にエラーを報告
$this->assertTextPresent($text);

                          • -

6. テストしてみる

コマンドプロンプトを起動し、下記を実行する。

cd C:\xampp\php
phpunit D:\xampp\selenium\selenium-test\test.php

ディレクトリを指定した場合はディレクトリにある全てのテストが実行されます。


========================
参考:

これはすごい! Web案件必須 Selenium - 人気急上昇中自動テストツール
http://journal.mycom.co.jp/articles/2006/09/29/selenium/index.html
PHPUnitSelenium RCで機能テストをやってみる
http://www.exgear.jp/blog/2010/10/phpunit-seleniumrc/
 PHPUnitインストール
 http://www.phpunit.de/manual/3.4/ja/installation.html
http://www.phpunit.de/manual/3.4/ja/selenium.html
http://colo-ri.jp/develop/2008/03/selenium_rc_phpunit3.html
ブラウザを選ばずWebテストを自動化するSelenium
http://www.atmarkit.co.jp/fjava/rensai4/devtool07/devtool07_1.html
SeleniumでWebアプリケーションテストを自動化
http://thinkit.co.jp/free/article/0705/2/1/
Selenium徹底入門
D:\usr\document\WEB+DB PRESS総集編[Vol.1〜36]\wdp_pdf\vol36\vol36_041-072.pdf


WEBアプリ開発に便利な機能&負荷テストツール集
http://phpspot.org/blog/archives/2009/05/web_45.html