📜 ⬆️ ⬇️

The Story of a Day: PHPUnit, Selenium, Facebook

Today I want to talk about how I suffered from PHPUnit , Selenium RC and Facebook :)

So, I was given the task to make a system for daily testing of the site and posting bugs in bugzill (I’ll only tell you about the PHPUnit / Selenium RC bundle). Using a little google, I found two things suitable for php - SimpleTest and PHPUnit . Having smoked docks I stopped at PHPUnit . The choice fell on him because of the support of Selenium.
Because I didn’t work with PHPUnit or Selenium before, so I had to look for something to read (thanks also to the WanderingStar user for the article Unit Testing in PHP ). Having read to red eyes, I decided to do the first test :) But first you need to install everything:

pear channel-discover pear.phpunit.de
pear install phpunit/PHPUnit-beta


Next, download Selenium RC (selenium-remote-control-1.0.1-dist.zip), unpack it and launch in the directory selenium-server-1.0.1:
')
java -jar selenium-server.jar

Now let's start writing. The first test, as always, was taken from the documentation :

  1. <? php
  2. require_once 'PHPUnit / Extensions / SeleniumTestCase.php' ;
  3. class WebTest extends PHPUnit_Extensions_SeleniumTestCase
  4. {
  5. protected function setUp ()
  6. {
  7. $ this -> setBrowser ( '* firefox' );
  8. $ this -> setBrowserUrl ( 'http://www.example.com/' );
  9. }
  10. public function testTitle ()
  11. {
  12. $ this -> open ( 'http://www.example.com/' );
  13. $ this -> assertTitleEquals ( 'Example WWW Page' );
  14. }
  15. }
  16. ?>
* This source code was highlighted with Source Code Highlighter .


Save to the WebTest.php file and run: phpunit WebTest

We see how FireFox was launched, opened the page and was killed :) The result of the test:
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.


Error ... It is not good to write code with errors in the documentation ... Remembering that this is a beta release, I went to check the changelog . Found an interesting thing:
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()


Fix assertTitleEquals on assertTitle and re-run:
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.


That's fine, the test worked. got a mistake. Next, I would like to save a screenshot of the error. Make changes to the code:

  1. <? php
  2. require_once 'PHPUnit / Extensions / SeleniumTestCase.php' ;
  3. class WebTest extends PHPUnit_Extensions_SeleniumTestCase
  4. {
  5. protected $ captureScreenshotOnFailure = TRUE;
  6. protected $ screenshotPath = 'd: \ apache2 \ htdocs \ screenshots' ;
  7. protected $ screenshotUrl = 'http: // localhost / screenshots' ;
  8. protected function setUp ()
  9. {
  10. $ this -> setBrowser ( '* firefox' );
  11. $ this -> setBrowserUrl ( 'http://www.example.com/' );
  12. }
  13. public function testTitle ()
  14. {
  15. $ this -> open ( 'http://www.example.com/' );
  16. $ this -> assertTitle ( 'Example WWW Page' );
  17. }
  18. }
  19. ?>
* This source code was highlighted with Source Code Highlighter .


Run:

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.


This is already better. An example from the documentation earned :) I would like to proceed to testing the developed site. Without going into details, the site resembles something like Yahoo Answers with authorization through Facebook. Here is the jamb waiting for us ...

The site has a button for login. Looks like this:

Code:
  1. < a onclick = "FB.Connect.requireSession (function () {window.location = 'index.php';}); return false;" href = "#" >
  2. < img alt = "Connect" src = "http://static.ak.fbcdn.net/images/fbconnect/login-buttons/connect_light_medium_long.gif" id = "fb_login_image" />
  3. </ a >
* This source code was highlighted with Source Code Highlighter .


After clicking on this button, the user sees PopUp:


Selenium provides an opportunity to click on an element, for this you need to know its id or name (you can also search using xpath, but it does not suit me, because the element can be moved). As you can see, the id tag is missing (it is temporarily impossible to correct the site code). I had to look for other authorization options. Googling a bit, I came across this option . In short, we go through authorization on facebook and get cookies that we send to Selenium. I failed to implement it, because I remembered that even if the user is authorized on facebook, he still needs to click on the “Connect with Facebook” button. Then I went to smoke :)

On the balcony, an idea came to my mind - why not click on the picture, because we have its id. I decided to try (though I doubted that it would work, because onclick is not hanging in the picture, but there were no other ideas) Here is what I said:

  1. <? php
  2. require_once 'PHPUnit / Extensions / SeleniumTestCase.php' ;
  3. class LoginTest extends PHPUnit_Extensions_SeleniumTestCase
  4. {
  5. protected $ captureScreenshotOnFailure = TRUE;
  6. protected $ screenshotPath = 'd: \ apache2 \ htdocs \ screenshots' ;
  7. protected $ screenshotUrl = 'http: // localhost / screenshots' ;
  8. protected function setUp ()
  9. {
  10. $ this -> setBrowser ( '* firefox' );
  11. $ this -> setBrowserUrl ( 'http: //% site_url% /' );
  12. }
  13. public function testTitle ()
  14. {
  15. $ this -> open ( '/' );
  16. // Click Login
  17. $ this -> click ( "fb_login_image" );
  18. }
  19. }
  20. ?>
* This source code was highlighted with Source Code Highlighter .


What was my surprise when it worked ... Now we need to enter the email and password and click Connect. Id of these elements are known to us - email, pass, login. We make this code:

  1. <? php
  2. require_once 'PHPUnit / Extensions / SeleniumTestCase.php' ;
  3. class LoginTest extends PHPUnit_Extensions_SeleniumTestCase
  4. {
  5. protected $ captureScreenshotOnFailure = TRUE;
  6. protected $ screenshotPath = 'd: \ apache2 \ htdocs \ screenshots' ;
  7. protected $ screenshotUrl = 'http: // localhost / screenshots' ;
  8. protected function setUp ()
  9. {
  10. $ this -> setBrowser ( '* firefox' );
  11. $ this -> setBrowserUrl ( 'http: //% site_url% /' );
  12. }
  13. public function testTitle ()
  14. {
  15. $ this -> open ( '/' );
  16. // Click Login
  17. $ this -> click ( "fb_login_image" );
  18. $ this -> type ( "email" , "test@test.com" );
  19. $ this -> type ( "pass" , "password" );
  20. $ this -> click ( "login" );
  21. }
  22. }
  23. ?>
* This source code was highlighted with Source Code Highlighter .


Result:
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.


How so? He is ... A little thought, I still understand why not. This element is in PopUp and not in the main window. Looking in the documentation how to organize switching between windows I got the final result of the code:

  1. <? php
  2. require_once 'PHPUnit / Extensions / SeleniumTestCase.php' ;
  3. class LoginTest extends PHPUnit_Extensions_SeleniumTestCase
  4. {
  5. protected $ captureScreenshotOnFailure = TRUE;
  6. protected $ screenshotPath = 'd: \ apache2 \ htdocs \ screenshots' ;
  7. protected $ screenshotUrl = 'http: // localhost / screenshots' ;
  8. protected function setUp ()
  9. {
  10. $ this -> setBrowser ( '* firefox' );
  11. $ this -> setBrowserUrl ( 'http: //% site_url% /' );
  12. }
  13. public function testTitle ()
  14. {
  15. // Open the main page
  16. $ this -> open ( '/' );
  17. // Check the title (did we get to that site?)
  18. $ this -> assertTitle ( '% page_title%' );
  19. // Click login
  20. $ this -> click ( "fb_login_image" );
  21. // Waiting for popup to pop out
  22. $ this -> waitForPopUp ( "_blank" , "30000" );
  23. // Choose our popup
  24. $ this -> selectWindow ( "_blank" );
  25. // fill in the fields and click Connect
  26. $ this -> type ( "email" , "test@test.com" );
  27. $ this -> type ( "pass" , "password" );
  28. $ this -> click ( "login" );
  29. // Select the main window
  30. $ this -> selectWindow ( "null" );
  31. // Waiting for it to load
  32. $ this -> waitForPageToLoad ( "30000" );
  33. // Check the title
  34. $ this -> assertTitle ( '% page_title%' );
  35. // Check whether or not logged in (does the text signout exist on the page)
  36. $ this -> verifyTextPresent ( 'signout' );
  37. }
  38. }
* This source code was highlighted with Source Code Highlighter .


Thank you all for your attention :) Any criticism is welcome.

ZY Do not kick much, the first post after all. If the articles of such a plan are interesting, I can still write about something.

Source: https://habr.com/ru/post/64240/


All Articles