ワンデイストーリー:PHPUnit、Selenium、Facebook

今日は、 PHPUnitSelenium RC 、Facebookで苦しんだことについてお話したいと思います:)



そのため、サイトを毎日テストし、bugzillにバグを投稿するためのシステムを作成するタスクが与えられました(PHPUnit / Selenium RCバンドルについてのみ説明します)。 少しグーグルを使って、phpに適したものが2つあることがわかりました-SimpleTestPHPUnitです。 ドックを喫煙した後、 PHPUnitに決めました。 Selenium'aのサポートにより、選択は彼にかかった。

なぜなら PHPUnitやSeleniumを使ったことはありませんでしたが、 読むべきものを探す必要がありました( PHPでのユニットテストの記事について、ユーザーWanderingStarにも感謝します )。 赤目まで読んだので、私は最初のテストを行うことにしました:)しかし、最初にすべてをインストールする必要があります:



pear channel-discover pear.phpunit.de

pear install phpunit/PHPUnit-beta









次に、Selenium RC(selenium-remote-control-1.0.1-dist.zip)をダウンロードして展開し、selenium-server-1.0.1ディレクトリで実行します。



java -jar selenium-server.jar







さあ、書き始めましょう。 最初のテストは、いつものように、 ドキュメントから取得されました:







  1. <?php
  2. require_once 'PHPUnit / Extensions / SeleniumTestCase.php' ;
  3. WebTest クラスは PHPUnit_Extensions_SeleniumTestCaseを拡張します
  4. {
  5. 保護された関数setUp()
  6. {
  7. $ this-> setBrowser( '* firefox' );
  8. $ this-> setBrowserUrl( 'http://www.example.com/' );
  9. }
  10. パブリック関数testTitle()
  11. {
  12. $ this-> open( 'http://www.example.com/' );
  13. $ this-> assertTitleEquals( 'WWWページの例' );
  14. }
  15. }
  16. ?>
*このソースコードは、 ソースコードハイライターで強調表示されました。




WebTest.phpファイルに保存して実行します: phpunit WebTest







FireFoxがどのように起動し、ページを開き、殺されたかを確認します:)テストの結果:

PHPUnit 3.4.0beta4 by Sebastian Bergmann.



E



Time: 14 seconds



There was 1 error:



1) WebTest::testTitle

BadMethodCallException: Method assertTitleEquals not defined.

WebTest.php:15



FAILURES!

Tests: 1, Assertions: 0, Errors: 1.









エラー...ドキュメントにエラーのあるコードを書くのは良くありません...これはベータリリースであることを思い出して、 changelogをチェックしに行きました。 興味深いものを見つけました:

Please note that the following commands had to be renamed:



assertAlertPresent() has been renamed to assertAlert()

assertNoAlertPresent() has been renamed to assertNotAlert()

assertNoConfirmationPresent() has been renamed to assertConfirmationNotPresent()

assertLocationEquals() has been renamed to assertLocation()

assertLocationNotEquals() has been renamed to assertNotLocation()

assertNoPromptPresent() has been renamed to assertPromptNotPresent()

assertNothingSelected() has been renamed to assertNotSomethingSelected()

assertTitleEquals() has been renamed to assertTitle()

assertTitleNotEquals() has been renamed to assertNotTitle()









assertTitleEqualsをassertTitleに修正し、再度実行します。

PHPUnit 3.4.0beta4 by Sebastian Bergmann.



F



Time: 9 seconds



There was 1 failure:



1) WebTest::testTitle

Current URL: www.example.com



Failed asserting that <string:Example Web Page> matches PCRE pattern "/Example WWW Page/".

WebTest.php:15



FAILURES!

Tests: 1, Assertions: 1, Failures: 1.









それは素晴らしい、テストはうまくいった。 エラーが発生しました。 次に、エラーのスクリーンショットを保存したいと思います。 コードに変更を加えます。







  1. <?php
  2. require_once 'PHPUnit / Extensions / SeleniumTestCase.php' ;
  3. WebTest クラスは PHPUnit_Extensions_SeleniumTestCaseを拡張します
  4. {
  5. protected $ captureScreenshotOnFailure = TRUE;
  6. protected $ screenshotPath = 'd:\ apache2 \ htdocs \ screenshots' ;
  7. protected $ screenshotUrl = 'http:// localhost / screenshots' ;
  8. 保護された関数setUp()
  9. {
  10. $ this-> setBrowser( '* firefox' );
  11. $ this-> setBrowserUrl( 'http://www.example.com/' );
  12. }
  13. パブリック関数testTitle()
  14. {
  15. $ this-> open( 'http://www.example.com/' );
  16. $ this-> assertTitle( 'WWWページの例' );
  17. }
  18. }
  19. ?>
*このソースコードは、 ソースコードハイライターで強調表示されました。




以下を開始します。



PHPUnit 3.4.0beta4 by Sebastian Bergmann.



F



Time: 8 seconds



There was 1 failure:



1) WebTest::testTitle

Current URL: www.example.com

Screenshot: localhost/screenshots/ed26432dcfb69cbbdd4e0c01fade4682.png



Failed asserting that <string:Example Web Page> matches PCRE pattern "/Example WWW Page/".

WebTest.php:19



FAILURES!

Tests: 1, Assertions: 1, Failures: 1.









それはすでに良いです。 ドキュメントからの例はうまくいきました:)開発中のサイトのテストに進みたいと思います。 詳細を説明するまではありませんが、このサイトはFacebookの承認を受けたYahoo Answersに似ています。 ここでは、わき柱が私たちを待っています...



サイトにはログイン用のボタンがあります。 次のようになります。



コード:





  1. < a onclick = "FB.Connect.requireSession(function(){window.location = 'index.php';}); return false;" href = "#" >
  2. < img alt = "接続" src = "http://static.ak.fbcdn.net/images/fbconnect/login-buttons/connect_light_medium_long.gif" id = "fb_login_image" />
  3. </ a >
*このソースコードは、 ソースコードハイライターで強調表示されました。




このボタンをクリックすると、ポップアップが表示されます:





Seleniumは要素をクリックする機能を提供します。これには、IDまたは名前を知る必要があります(xpathを使用して検索することも可能ですが、要素は移動できるため、私には向いていません)。 ご覧のとおり、idタグがありません(一時的にサイトコードを修正することはできません)。 他の認証オプションを探す必要がありました。 少しグーグルで、 このオプションに出くわしました 。 要するに、Facebookの承認を経て、Seleniumに提供されるCookieを取得します。 私はこれを実現できませんでした ユーザーがFacebookにログインしていても、「Facebookに接続」ボタンをクリックする必要があることを思い出しました。 それから私は喫煙に行きました:)



バルコニーで、アイディアが浮かんできました。写真をクリックしてみてください。 私は試してみることにしました(onclickが画像にハングアップしないため、それが機能するかどうかは本当に疑いましたが、他のアイデアはありませんでした)。 これが私がコンパイルしたものです:







  1. <?php
  2. require_once 'PHPUnit / Extensions / SeleniumTestCase.php' ;
  3. クラス LoginTestはPHPUnit_Extensions_SeleniumTestCaseを拡張します
  4. {
  5. protected $ captureScreenshotOnFailure = TRUE;
  6. protected $ screenshotPath = 'd:\ apache2 \ htdocs \ screenshots' ;
  7. protected $ screenshotUrl = 'http:// localhost / screenshots' ;
  8. 保護された関数setUp()
  9. {
  10. $ this-> setBrowser( '* firefox' );
  11. $ this- > setBrowserUrl( 'http://%site_url%/' );
  12. }
  13. パブリック関数testTitle()
  14. {
  15. $ this-> open( '/' );
  16. // [ログイン]をクリックします
  17. $ this- > click( "fb_login_image" );
  18. }
  19. }
  20. ?>
*このソースコードは、 ソースコードハイライターで強調表示されました。




うまくいったときの驚きを想像してみてください...ここで、電子メールとパスワードを入力し、[接続]をクリックする必要があります。 これらの要素のID(電子メール、パス、ログイン)を知っています。 次のコードを作成します。







  1. <?php
  2. require_once 'PHPUnit / Extensions / SeleniumTestCase.php' ;
  3. クラス LoginTestはPHPUnit_Extensions_SeleniumTestCaseを拡張します
  4. {
  5. protected $ captureScreenshotOnFailure = TRUE;
  6. protected $ screenshotPath = 'd:\ apache2 \ htdocs \ screenshots' ;
  7. protected $ screenshotUrl = 'http:// localhost / screenshots' ;
  8. 保護された関数setUp()
  9. {
  10. $ this-> setBrowser( '* firefox' );
  11. $ this- > setBrowserUrl( 'http://%site_url%/' );
  12. }
  13. パブリック関数testTitle()
  14. {
  15. $ this-> open( '/' );
  16. // [ログイン]をクリックします
  17. $ this- > click( "fb_login_image" );
  18. $ this- > type( "email""test@test.com" );
  19. $ this-> type( "pass""password" );
  20. $ this- >クリック( "ログイン" );
  21. }
  22. }
  23. ?>
*このソースコードは、 ソースコードハイライターで強調表示されました。




結果:

PHPUnit 3.4.0beta4 by Sebastian Bergmann.



E



Time: 25 seconds



There was 1 error:



1) WebTest::testTitle

RuntimeException: Response from Selenium RC server for testComplete().

ERROR: Element email not found.



WebTest.php:32



FAILURES!

Tests: 1, Assertions: 1, Errors: 1.









どうして? 彼は...少し考えた後、私はまだそうでないことに気づきました。 この要素はメインウィンドウではなく、ポップアップにあります。 ウィンドウ間の切り替えを整理する方法に関するドキュメントを見て、コードの最終結果が得られました。







  1. <?php
  2. require_once 'PHPUnit / Extensions / SeleniumTestCase.php' ;
  3. クラス LoginTestはPHPUnit_Extensions_SeleniumTestCaseを拡張します
  4. {
  5. protected $ captureScreenshotOnFailure = TRUE;
  6. protected $ screenshotPath = 'd:\ apache2 \ htdocs \ screenshots' ;
  7. protected $ screenshotUrl = 'http:// localhost / screenshots' ;
  8. 保護された関数setUp()
  9. {
  10. $ this-> setBrowser( '* firefox' );
  11. $ this- > setBrowserUrl( 'http://%site_url%/' );
  12. }
  13. パブリック関数testTitle()
  14. {
  15. //メインページを開きます
  16. $ this-> open( '/' );
  17. //タイトルを確認します(そのサイトに着きましたか?)
  18. $ this-> assertTitle( '%page_title%' );
  19. //ログインをクリックします
  20. $ this- > click( "fb_login_image" );
  21. //ポップアップが表示されるまで待ちます
  22. $ this- > waitForPopUp( "_blank""30000" );
  23. //ポップアップを選択
  24. $ this-> selectWindow( "_blank" );
  25. //フィールドに入力して、[接続]をクリックします
  26. $ this- > type( "email""test@test.com" );
  27. $ this-> type( "pass""password" );
  28. $ this- >クリック( "ログイン" );
  29. //メインウィンドウを選択します
  30. $ this-> selectWindow( "null" );
  31. //ロードされるのを待ちます
  32. $ this-> waitForPageToLoad( "30000" );
  33. //タイトルを確認します
  34. $ this-> assertTitle( '%page_title%' );
  35. //ログインしているかどうかを確認します(ページにサインアウトテキストがあります)
  36. $ this-> verifyTextPresent( 'signout' );
  37. }
  38. }
*このソースコードは、 ソースコードハイライターで強調表示されました。




ご清聴ありがとうございました:)どんな批判も歓迎します。



Z.Y. 結局、キックしないでください。 そのような計画の記事が興味深い場合、私はまだ何かについて書くことができます。






All Articles