UA testing with Selenium and PHPUnit - PFCongres 2013

56
UA Tes’ng with Selenium and PHPUnit PFCongres 2013 Utrecht

description

Nothing is as frustrated as deploying a new release of your web application to find out functionality you had doesn't work anymore. Of course you have all your unit tests in place and you run them through your CI environment, but nothing prepared you to a failing javascript error or a link that doesn't work anymore. Welcome to User Acceptance testing or UAT. Before you start putting real people in front of your application, create macros and export them as PHPUnit test classes. Then run them in an automated way just like your unit tests and hook them into your CI. In this talk I will show you how easy it is to create Selenium macros that can be converted into PHPUnit scripts and run automatically on different virtual machines (VM's) so you can test all different browsers on a diversity of operating systems.

Transcript of UA testing with Selenium and PHPUnit - PFCongres 2013

Page 1: UA testing with Selenium and PHPUnit - PFCongres 2013

UA  Tes'ng  withSelenium  and  PHPUnit

PFCongres  2013  -­‐  Utrecht

Page 2: UA testing with Selenium and PHPUnit - PFCongres 2013

2

• PHP  Consultant• President  PHPBenelux• Conference  speaker

Michelangelo  van  Dam

Page 3: UA testing with Selenium and PHPUnit - PFCongres 2013

3

Page 4: UA testing with Selenium and PHPUnit - PFCongres 2013

Today’s  goal

• Set  up  and  use  Selenium  IDE• Record  UA  tests• Convert  to  PHPUnit• Run  con'nuously• Mul'  browser  support

4

Page 5: UA testing with Selenium and PHPUnit - PFCongres 2013

5

⚠DISCLAIMERS E L E N I U M T E S T S A R E N OT A REPLACEMENT FOR REGULAR UNIT TESTING. THEY ONLY PROVIDE AN ADDITIONAL SET OF TESTS FOCUSED ON USER ACCEPTANCE AND USER EXPERIENCE TESTING.

For more information about unit testing, please see my other material on www.slideshare.net and www.speakerdeck.com. Search for “dragonbe”!

Page 6: UA testing with Selenium and PHPUnit - PFCongres 2013

User  Acceptance

6

Page 7: UA testing with Selenium and PHPUnit - PFCongres 2013

7

“Acceptance testing is a test conducted to determine if the requirements of a specification or contract are met.”

-- source: wikipedia

Page 8: UA testing with Selenium and PHPUnit - PFCongres 2013

Checklist  for  web  applica'ons

8

Page 9: UA testing with Selenium and PHPUnit - PFCongres 2013

9

Func'onal  tes'ng

• Test  func'onal  requirements-­‐ e.g.  no  access  to  profile  without  authen'ca'on

• Test  UI  elements  on  the  web  interface-­‐ e.g.  buRons,  form  elements,  AJAX  controls,  …

Page 10: UA testing with Selenium and PHPUnit - PFCongres 2013

A  word  of  cau'on!

10

• UA  tests  only  test  generated  output-­‐ not  a  replacement  for  unit  tes'ng

• UA  tests  are  heavily  depending  on  DOM-­‐ changes  to  the  DOM  might  lead  to  failing  UAT

Page 11: UA testing with Selenium and PHPUnit - PFCongres 2013

Browser  support

11

Page 12: UA testing with Selenium and PHPUnit - PFCongres 2013

Selenium  to  the  rescue

12

Page 13: UA testing with Selenium and PHPUnit - PFCongres 2013

Plugin  for  firefox

13

Page 14: UA testing with Selenium and PHPUnit - PFCongres 2013

Get  the  plugin  (demo)

14

Page 15: UA testing with Selenium and PHPUnit - PFCongres 2013
Page 16: UA testing with Selenium and PHPUnit - PFCongres 2013

Let’s  get  started

16

Page 17: UA testing with Selenium and PHPUnit - PFCongres 2013

Pick  a  test  case

17

Page 18: UA testing with Selenium and PHPUnit - PFCongres 2013

Issue  #7

18

Page 19: UA testing with Selenium and PHPUnit - PFCongres 2013

Verify  this  issue  on  PROD

19

Page 20: UA testing with Selenium and PHPUnit - PFCongres 2013

20

Page 21: UA testing with Selenium and PHPUnit - PFCongres 2013

Fix  the  issue

21

Page 22: UA testing with Selenium and PHPUnit - PFCongres 2013

Run  test  to  see  it’s  fixed

22

Page 23: UA testing with Selenium and PHPUnit - PFCongres 2013

23

Page 24: UA testing with Selenium and PHPUnit - PFCongres 2013

24

Save  your  test  as  .html

Page 25: UA testing with Selenium and PHPUnit - PFCongres 2013

It’s  that  easy!

25

Page 26: UA testing with Selenium and PHPUnit - PFCongres 2013

Automated  Tes'ng

26

Page 27: UA testing with Selenium and PHPUnit - PFCongres 2013

PHPUnit  to  the  rescue

27

Page 28: UA testing with Selenium and PHPUnit - PFCongres 2013

Export  to  PHPUnit

28

Page 29: UA testing with Selenium and PHPUnit - PFCongres 2013

The  PHPUnit  TestCase

29

<?phpclass Example extends PHPUnit_Extensions_SeleniumTestCase{ protected function setUp() { $this->setBrowser("*chrome"); $this->setBrowserUrl("http://www.theialive.com/"); }

public function testMyTestCase() { $this->open("/"); $this->click("link=login"); $this->waitForPageToLoad("30000"); $this->type("id=email", "[email protected]"); $this->type("id=password", "test1234"); $this->click("id=signin"); $this->waitForPageToLoad("30000"); $this->click("link=Test demo"); $this->waitForPageToLoad("30000"); $this->assertEquals("Done", $this->getText("xpath=//th[5]")); $this->click("link=[EDIT]"); $this->waitForPageToLoad("30000"); $this->assertTrue($this->isElementPresent("id=done")); $this->click("link=sign off"); $this->waitForPageToLoad("30000"); }}?>

Page 30: UA testing with Selenium and PHPUnit - PFCongres 2013

Change  class  name

30

<?phpclass Example extends PHPUnit_Extensions_SeleniumTestCase{ protected function setUp() { $this->setBrowser("*chrome"); $this->setBrowserUrl("http://www.theialive.com/"); }

public function testMyTestCase() { $this->open("/"); $this->click("link=login"); $this->waitForPageToLoad("30000"); $this->type("id=email", "[email protected]"); $this->type("id=password", "test1234"); $this->click("id=signin"); $this->waitForPageToLoad("30000"); $this->click("link=Test demo"); $this->waitForPageToLoad("30000"); $this->assertEquals("Done", $this->getText("xpath=//th[5]")); $this->click("link=[EDIT]"); $this->waitForPageToLoad("30000"); $this->assertTrue($this->isElementPresent("id=done")); $this->click("link=sign off"); $this->waitForPageToLoad("30000"); }}?>

class MarkTaskDoneTest extends PHPUnit_Extensions_SeleniumTestCase

Page 31: UA testing with Selenium and PHPUnit - PFCongres 2013

The  PHPUnit  TestCase

31

<?phpclass MarkTaskDoneTest extends PHPUnit_Extensions_SeleniumTestCase{ protected function setUp() { $this->setBrowser("*chrome"); $this->setBrowserUrl("http://www.theialive.com/"); }

public function testMyTestCase() { $this->open("/"); $this->click("link=login"); $this->waitForPageToLoad("30000"); $this->type("id=email", "[email protected]"); $this->type("id=password", "test1234"); $this->click("id=signin"); $this->waitForPageToLoad("30000"); $this->click("link=Test demo"); $this->waitForPageToLoad("30000"); $this->assertEquals("Done", $this->getText("xpath=//th[5]")); $this->click("link=[EDIT]"); $this->waitForPageToLoad("30000"); $this->assertTrue($this->isElementPresent("id=done")); $this->click("link=sign off"); $this->waitForPageToLoad("30000"); }}?>

protected function setUp() { $this->setBrowser("*iexplore"); $this->setBrowserUrl("http://www.theialive.com/"); $this->setHost('192.168.56.101'); $this->setPort(12666); }

Page 32: UA testing with Selenium and PHPUnit - PFCongres 2013

Meaningful  method  name<?phpclass MarkTaskDoneTest extends PHPUnit_Extensions_SeleniumTestCase{ protected function setUp() { $this->setBrowser("*iexplore"); $this->setBrowserUrl("http://www.theialive.com/"); $this->setHost('192.168.56.101'); $this->setPort(12666); }

public function testMyTestCase() { $this->open("/"); $this->click("link=login"); $this->waitForPageToLoad("30000"); $this->type("id=email", "[email protected]"); $this->type("id=password", "test1234"); $this->click("id=signin"); $this->waitForPageToLoad("30000"); $this->click("link=Test demo"); $this->waitForPageToLoad("30000"); $this->assertEquals("Done", $this->getText("xpath=//th[5]")); $this->click("link=[EDIT]"); $this->waitForPageToLoad("30000"); $this->assertTrue($this->isElementPresent("id=done")); $this->click("link=sign off"); $this->waitForPageToLoad("30000"); }}?>

32

public function testMarkTestAsDone()

Page 33: UA testing with Selenium and PHPUnit - PFCongres 2013

startSeleniumStandAlone.BAT

33

"C:\Program Files\Java\jre7\bin\java.exe" -jar "C:\Users\user\Downloads\selenium-server-standalone-2.28.0.jar" -port 12666

Page 34: UA testing with Selenium and PHPUnit - PFCongres 2013

Now  run  your  tests

34

Page 35: UA testing with Selenium and PHPUnit - PFCongres 2013
Page 36: UA testing with Selenium and PHPUnit - PFCongres 2013

How  it  runs  on  the  node

36

Page 37: UA testing with Selenium and PHPUnit - PFCongres 2013
Page 38: UA testing with Selenium and PHPUnit - PFCongres 2013

Advantages

38

• You  can  start  tes'ng  immediately• Even  test  “hard  to  test”  kind  of  situa'ons• More  nodes  for  parallel  tes'ng• Tes'ng  different  browsers  and  plaeorms• Con'nuous  Integra'on  possible

Page 39: UA testing with Selenium and PHPUnit - PFCongres 2013

Selenium  Grid  Setup

39

Selenium Testing

CI Server Windows"HUB"

Linux client"NODE"

CI executes tests

Windows HUB launchesSelenium node clients to execute tests

Windows Server collectsfeedback from the Citrixclient nodes and reportsback to CI Server

Windows client"NODE"

Mac OS X client"NODE"

Continuous User Acceptance Testing

Page 40: UA testing with Selenium and PHPUnit - PFCongres 2013

Next  Steps

40

Page 41: UA testing with Selenium and PHPUnit - PFCongres 2013

Mul'  Browser  support

41

Page 42: UA testing with Selenium and PHPUnit - PFCongres 2013

Base  TestCase

42

<?php

require_once 'PHPUnit/Extensions/SeleniumTestCase.php';

class TestCase extends PHPUnit_Extensions_SeleniumTestCase{ //const TEST_HUB = '217.21.179.192'; const TEST_HUB = '192.168.56.101'; const TEST_PORT = 12666;

const USERNAME = '[email protected]'; const PASSWORD = 'test1234'; const BASURL = 'http://www.theialive.com';

public static $browsers = array ( array ( 'name' => 'Internet Explorer 8 on Windows 7', 'browser' => '*iexplore', 'host' => self::TEST_HUB, 'port' => self::TEST_PORT, ), array ( 'name' => 'Firefox on Windows 7', 'browser' => '*firefox', 'host' => self::TEST_HUB, 'port' => self::TEST_PORT, ), array ( 'name' => 'Google Chrome on Windows 7', 'browser' => '*googlechrome', 'host' => self::TEST_HUB, 'port' => self::TEST_PORT, ), );

protected function setUp() { $this->setBrowserUrl(self::BASURL); }}

Page 43: UA testing with Selenium and PHPUnit - PFCongres 2013

Base  TestCase

43

<?php

require_once 'PHPUnit/Extensions/SeleniumTestCase.php';

class TestCase extends PHPUnit_Extensions_SeleniumTestCase{ //const TEST_HUB = '217.21.179.192'; const TEST_HUB = '192.168.56.101'; const TEST_PORT = 12666;

const USERNAME = '[email protected]'; const PASSWORD = 'test1234'; const BASURL = 'http://www.theialive.com';

public static $browsers = array ( array ( 'name' => 'Internet Explorer 8 on Windows 7', 'browser' => '*iexplore', 'host' => self::TEST_HUB, 'port' => self::TEST_PORT, ), array ( 'name' => 'Firefox on Windows 7', 'browser' => '*firefox', 'host' => self::TEST_HUB, 'port' => self::TEST_PORT, ), array ( 'name' => 'Google Chrome on Windows 7', 'browser' => '*googlechrome', 'host' => self::TEST_HUB, 'port' => self::TEST_PORT, ), );

protected function setUp() { $this->setBrowserUrl(self::BASURL); }}

array ( 'name' => 'Internet Explorer 8 on Windows 7', 'browser' => '*iexplore', 'host' => self::TEST_HUB, 'port' => self::TEST_PORT,),

Page 44: UA testing with Selenium and PHPUnit - PFCongres 2013

Base  TestCase

44

<?php

require_once 'PHPUnit/Extensions/SeleniumTestCase.php';

class TestCase extends PHPUnit_Extensions_SeleniumTestCase{ //const TEST_HUB = '217.21.179.192'; const TEST_HUB = '192.168.56.101'; const TEST_PORT = 12666;

const USERNAME = '[email protected]'; const PASSWORD = 'test1234'; const BASURL = 'http://www.theialive.com';

public static $browsers = array ( array ( 'name' => 'Internet Explorer 8 on Windows 7', 'browser' => '*iexplore', 'host' => self::TEST_HUB, 'port' => self::TEST_PORT, ), array ( 'name' => 'Firefox on Windows 7', 'browser' => '*firefox', 'host' => self::TEST_HUB, 'port' => self::TEST_PORT, ), array ( 'name' => 'Google Chrome on Windows 7', 'browser' => '*googlechrome', 'host' => self::TEST_HUB, 'port' => self::TEST_PORT, ), );

protected function setUp() { $this->setBrowserUrl(self::BASURL); }}

array ( 'name' => 'Firefox on Windows 7', 'browser' => '*firefox', 'host' => self::TEST_HUB, 'port' => self::TEST_PORT,),

Page 45: UA testing with Selenium and PHPUnit - PFCongres 2013

Base  TestCase

45

<?php

require_once 'PHPUnit/Extensions/SeleniumTestCase.php';

class TestCase extends PHPUnit_Extensions_SeleniumTestCase{ //const TEST_HUB = '217.21.179.192'; const TEST_HUB = '192.168.56.101'; const TEST_PORT = 12666;

const USERNAME = '[email protected]'; const PASSWORD = 'test1234'; const BASURL = 'http://www.theialive.com';

public static $browsers = array ( array ( 'name' => 'Internet Explorer 8 on Windows 7', 'browser' => '*iexplore', 'host' => self::TEST_HUB, 'port' => self::TEST_PORT, ), array ( 'name' => 'Firefox on Windows 7', 'browser' => '*firefox', 'host' => self::TEST_HUB, 'port' => self::TEST_PORT, ), array ( 'name' => 'Google Chrome on Windows 7', 'browser' => '*googlechrome', 'host' => self::TEST_HUB, 'port' => self::TEST_PORT, ), );

protected function setUp() { $this->setBrowserUrl(self::BASURL); }}

array ( 'name' => 'Google Chrome on Windows 7', 'browser' => '*googlechrome', 'host' => self::TEST_HUB, 'port' => self::TEST_PORT,),

Page 46: UA testing with Selenium and PHPUnit - PFCongres 2013

Modify  MarkTaskDoneTest  <?php/** * Class MarkTaskDoneTest * * @group Selenium */require_once 'TestCase.php';class MarkTaskDoneTest extends TestCase{ public function testMarkTestAsDone() { $this->open("/"); $this->click("link=login"); $this->waitForPageToLoad("30000"); $this->type("id=email", TestCase::USERNAME); $this->type("id=password", TestCase::PASSWORD); $this->click("id=signin"); $this->waitForPageToLoad("30000"); $this->click("link=Test demo"); $this->waitForPageToLoad("30000"); $this->assertEquals("Done", $this->getText("xpath=//th[5]")); $this->click("link=[EDIT]"); $this->waitForPageToLoad("30000"); $this->assertTrue($this->isElementPresent("id=done")); $this->click("link=sign off"); $this->waitForPageToLoad("30000"); }}

46

Require the TestCaseand extend it

Page 47: UA testing with Selenium and PHPUnit - PFCongres 2013

Running  test

47

Page 48: UA testing with Selenium and PHPUnit - PFCongres 2013

48

Page 49: UA testing with Selenium and PHPUnit - PFCongres 2013

Benefits

• run  your  tests  on  mul'ple  browsers• detect  flaws  in  specific  browsers  (e.g.  IE6)-­‐ adapt  your  apps  to  solve  these  flaws

49

Page 50: UA testing with Selenium and PHPUnit - PFCongres 2013

More  informa'on

50

Page 51: UA testing with Selenium and PHPUnit - PFCongres 2013

seleniumhq.org

51

Page 52: UA testing with Selenium and PHPUnit - PFCongres 2013

phpunit.de

52

http://www.phpunit.de/manual/3.5/en/selenium.html

Page 53: UA testing with Selenium and PHPUnit - PFCongres 2013

Credits

53

• apple  store:  hRp://www.flickr.com/photos/jtjdt/3571748777• checklist:  hRp://www.flickr.com/photos/alancleaver/

4439276478• flat  're:  hRp://www.flickr.com/photos/anijdam/2468493546/• first  place:  hRp://www.flickr.com/photos/evelynishere/

3417340248/• gears:  hRp://www.flickr.com/photos/wwarby/4782904694• steps:  hRp://www.flickr.com/photos/ben_salter/1407168763• browsers:  hRp://www.flickr.com/photos/richoz/3791167457• informa'on:  hRp://www.flickr.com/photos/twicepix/

2650241408/• elephpant:  hRp://www.flickr.com/photos/drewm/3191872515

Page 54: UA testing with Selenium and PHPUnit - PFCongres 2013

54

Michelangelo van DamZend Certified Engineer

[email protected]

PHP Consulting - QA Audits - Training Courses

www.in2it.be

Page 55: UA testing with Selenium and PHPUnit - PFCongres 2013

55

9221

If you liked this talk, thanks!If you didn’t like it, let me know how to improve!

Page 56: UA testing with Selenium and PHPUnit - PFCongres 2013

Thank  you

56