MacOSでselenium2/firefoxブラウザーに嵌って抜け出した話

概要

  • MacOSfirefoxのdriver関係でこけるときは、firefoxのバージョンを下げてみる。
  • TravisCIでphpunit/selenium2テストを動かしたいとき、php5.4以上であればphpのbuiltinserverも使える。

MacOSでselenium2テスト 準備

いろいろ調べたけれども、以下の方法が一番簡単だと思う。

アプリケーションインストール

  • Homebrew(割愛
  • java-jdk(割愛
  • selenium-server-standalone brewでインストールしておくと起動のためのshellも用意してくれる
$ brew install selenium-server-standalone
$ cat composer.json  | grep phpunit
        "phpunit/phpunit": "4.1.*",
        "phpunit/phpunit-selenium": ">=1.2"

$ php composer.phar update

サーバ起動

logファイル・リダイレクトは適宜

  • selenium-standalone-server オプションなしで、localhost:4444 に立ち上がる
$ selenium-server >> log 2>&1 &
  • php ビルトインサーバー
$ php -S localhost:8888 -t $DOCUMENT_ROOT >> log 2>&1 &

selenium2のテストを用意

  • firefoxでトップページにアクセスして、ログインしてみる
  • (selenium2のアサーション、どうしたらベターかわからない・・)
class Test_View extends \PHPUnit_Extensions_Selenium2TestCase
{
  public static $browsers = array(
    array(
      'browserName' => 'firefox',
      'host' => 'localhost',
      'port' => 4444,
    ),
  );

  protected function setUp()
  {
    $this->setBrowserUrl('http://localhost:8888/');
  }

  public function test_sample()
  {
    $this->waitUntil(function() {
      $this->url('/');
      $this->byName('username')->value('test_user');
      $this->byName('password')->value('password');
      $this->byName('login')->submit();

      return true;
    }, 60000);
}

テスト実施

$ phpunit
Tests Running...This may take a few moments.
PHPUnit 4.1.6 by Sebastian Bergmann.

Configuration read from /Users/tyoshii/git/tyoshii/bms/fuel/app/phpunit.xml

.

Time: 7.1 seconds, Memory: 17.75Mb

OK (1 tests, 0 assertions)

トラブルシューティング

phpunit実施した際に以下の用なエラーが出て嵌りました。

Cannot find firefox binary in PATH. Make sure firefox is installed.
Unable to connect to host 127.0.0.1 on port 7055 after 45000 ms. Firefox console output:

seleniumにはfirefoxのドライバーはデフォルトで入っていると書いてあるが、macの場合は・・・?

Cannnot find ...のエラーは、そもそもFireFoxブラウザーがダウンロードされていないときに出ました。 なのでFireFoxを普通にダウンロードするものの、Unable to connect ...のエラーが。

このエラーの解決に少し嵌ったが、とりあえずFIreFoxのバージョンをダウングレードすることで解決した。 この記事を書いた時点でFireFoxの最新バージョンは32.0だったが、31.1に落とすことでエラーがとれました。

MacOS FireFox 31.1 https://ftp.mozilla.org/pub/mozilla.org/firefox/releases/31.1.0esr/mac/ja-JP-mac/

TravisCIで動かす

Travis CI上でPHPアプリのWebベースのテストを自動化する - Engine Yard Blog

上記を参考に設定。

TravisCIはLinuxOSなので、その通りにselenium-serverはwgetで取得 => java -jarで起動。 (selenium-serverのバージョンは、一応いまの最新に)

php5.4以上を使っている場合は上述とおりビルトインサーバが使えそう。 before_scriptにあるapache2/php-fpm/vhostsなどは、以下1行に書き換える。

php -S localhost:8888 -t public > builtinserver.log 2>&1 &