More Related Content
Similar to LaravelアプリケーションをSeleniumでテストしてみた (20)
More from Yuta Ohashi (18)
LaravelアプリケーションをSeleniumでテストしてみた
- 1. Copyright © 2015 Yuta Ohashi All Rights Reserved.
Laravelアプリケーションを
Seleniumでテストしてみた
Laravel Meetup Tokyo vol.6
2015.06.12
Yuta Ohashi
- 3. 3Copyright © 2014 rocros All Rights Reserved.
Seleniumっていろいろある?
Selenium RC
Selenium IDE
Selenium WebDriver
…etc
- 4. 4Copyright © 2014 rocros All Rights Reserved.
Seleniumっていろいろある?
Selenium RC
Selenium IDE
Selenium WebDriver
…etc
- 6. 6Copyright © 2014 rocros All Rights Reserved.
https://github.com/blue-goheimochi/laravel-selenium-server
- 8. 8Copyright © 2014 rocros All Rights Reserved.
やったこと
PHPUnit経由でSelenium Serverから
FireFoxを実行しブラウザテスト
• PHPUnitを実行
↓
• Selenium Server から FireFox 起動
↓
• Laravelアプリケーションにアクセス
↓
• 結果を表示
- 9. 9Copyright © 2014 rocros All Rights Reserved.
やったこと詳細
composer.jsonの修正
テストの作成
サーバー環境構築
- 10. 10Copyright © 2014 rocros All Rights Reserved.
composer.jsonの修正
"require-dev": {
"phpunit/phpunit": "~4.0",
"phpspec/phpspec": "~2.1“,
"phpunit/phpunit-selenium": ">=1.3.1“
},
“phpunit/phpunit-selenium”: “>=1.3.1“ を追加。
- 11. 11Copyright © 2014 rocros All Rights Reserved.
テストの作成
<?php
class ExampleSeleniumTest extends PHPUnit_Extensions_Selenium2TestCase {
public $captureSSPath = "/vagrant/src";
protected function setUp()
{
$this->setBrowser('firefox');
$this->setBrowserUrl('http://192.168.33.6/');
}
public function testShowTopPage()
{
$this->url("/");
$this->writeScreenShot( 'top' );
}
public function writeScreenShot( $imgName ) ・・・・・・・・・続く
PHPUnit_Extensions_Selenium2TestCaseを継承した
テストを作成
- 12. 12Copyright © 2014 rocros All Rights Reserved.
サーバー環境構築
必要なものをインストール&実行
• FireFox
• Xvfb
• Selenium Server
- 15. 15Copyright © 2014 rocros All Rights Reserved.
できること
Seleniumを使ったテストでできること
• DOMで呼び出し(id,class,xpath)してブラウザテスト
• テストデータとかjsonとかで読み込んで使う
• (エラー時に)スクリーンショット撮れる
などなど・・・
実は自分はテストデータ作成とかで使っている。。
Laravelはぶっちゃけ関係ない・・・
- 16. 16Copyright © 2014 rocros All Rights Reserved.
Laravelはぶっちゃけ関係ない・・・
<?php
class ExampleSeleniumTest extends PHPUnit_Extensions_Selenium2TestCase {
public $captureSSPath = "/vagrant/src";
protected function setUp()
{
$this->setBrowser('firefox');
$this->setBrowserUrl('http://192.168.33.6/');
}
public function testShowTopPage()
{
$this->url("/");
$this->writeScreenShot( 'top' );
}
public function writeScreenShot( $imgName ) ・・・・・・・・・続く
URLの指定を変えればテスト対象も替わる
- 17. 17Copyright © 2014 rocros All Rights Reserved.
なので・・・
E2Eからテストをはじめてみてもよいかも!
既存のWebアプリケーションあるけどUnitテストそろえるのつらいな・・・
とかそういう状況であればSeleniumを使ったE2Eのテストからはじめてみるのも
いいのかなぁ・・・
とか思っていて、実際にそういう方向から個人的には攻めたりもしています。