LoginSignup
10
9

More than 5 years have passed since last update.

windowsでPHPでSelenium webdriverを使ってブラウザテスト

Posted at

windowsでPHPでSelenium webdriverを使ってブラウザテスト

事前準備

composerがインストール済み
Javaがインストール済み
PHPがインストール済み

手順

  1. Selenium Serverを立ち上げ、nodeを登録

selenium-server-standalone-2.52.0.jarがあるディレクトリまで移動して下記コマンドを実行

C:\D\selenium>java -jar selenium-server-standalone-2.52.0.jar -role hub -port 4445
C:\D\selenium>java -jar selenium-server-standalone-2.52.0.jar -role node  -hub http://localhost:4445/grid/register

2. phpunit, facebook/webdriverをcomposer経由でインストールする
テストコードがあるディレクトリまで移動して下記コマンドを実行。
facebook/webdriverでPHPでseleniumを実行できる

composer require phpunit/phpunit
composer require facebook/webdriver

3. テストスクリプト(webTest.php)を書く

php
<?php
use Facebook\WebDriver;
use Facebook\WebDriver\WebDriverExpectedCondition;
use Facebook\WebDriver\WebDriverBy;
use Facebook\WebDriver\Remote;
use Facebook\WebDriver\Remote\RemoteWebDriver;
use Facebook\WebDriver\Remote\DesiredCapabilities;

class webTest extends PHPUnit_Framework_TestCase
{
    public function testWebUI()
    {
        //hostの登録
        $host = 'http://localhost:4445/wd/hub';
        //どのブラウザを使うか設定
        $driver = RemoteWebDriver::create($host, DesiredCapabilities::firefox());
        //テストするサイトに移動
        $driver->get('http://192.168.33.51/');
        //idがtest-idの要素を取得
        $testId = $driver->findElement(
            WebDriverBy::id('test-id')
        );

        //test-idのテキストを取得する
        $text = $testId->getText();
        //期待値とあっているかのテスト
        $this->assertSame('Expected', $text);
        //ブラウザを閉じる
        //$driver->close();
    }
}

テストに使用するページのtest-idのDIVのテキストはtest-valueとする

<div id="test-id">test-value</div>

4. テストの実行
テストスクリプトがあるディレクトリまで移動して下記コマンドを実行

C:\D\vagrant\laravel51\selenium>vendor\bin\phpunit webTest
PHPUnit 5.2.5 by Sebastian Bergmann and contributors.

F                                                                  1 / 1 (100%)

Time: 29.34 seconds, Memory: 2.50Mb

There was 1 failure:

1) webTest::testWebUI
Failed asserting that two strings are identical.
--- Expected
+++ Actual
@@ @@
-Expected
+test-value

C:\D\vagrant\laravel51\selenium\webTest.php:23

FAILURES!
Tests: 1, Assertions: 1, Failures: 1.

テストした値はtest-valueで期待値がExpectedなのでNG

成功版は下記

C:\D\vagrant\laravel51\selenium>vendor\bin\phpunit webTest
PHPUnit 5.2.5 by Sebastian Bergmann and contributors.

.                                                                  1 / 1 (100%)

Time: 15.56 seconds, Memory: 2.50Mb

OK (1 test, 1 assertion)


10
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
9