PHPUnit + Selenium2 First Step Guide

PHPUnit + Selenium2 First Step Guide

Talk about PHPUnit + PHPUnit_Extensions_Selenium2TestCase

GitHub:
https://github.com/suzuki/phpunit-selenium2-samples

Heroku (Working Sample):
http://phpunit-selenium2-samples.herokuapp.com/

at Co-Edo, Japan:
https://www.facebook.com/CoWorkingSpaceKayabacho

8e528456ff66ec543952daa815353a01?s=128

Norio Suzuki

March 09, 2013
Tweet

Transcript

  1. 1)16OJU 4FMFOJVN 'JSTU4UFQ(VJEF !TV[VLJ  BU$P&EP

  2. "CPVUNF w5XJUUFS!TV[VLJ w(JU)VC 7JNFP 6TUSFBNTV[VLJ w8FC/JLLJIUUQTV[VLJUEJBSZOFU IUUQHJIZPKQMJGFTUZMFTFSJBMFWFOUTUSFBNJOH

  3. None
  4. "CPVUUIJTTFTTJPO

  5. w IUUQTHJUIVCDPNTV[VLJQIQVOJUTFMFOJVNTBNQMFT 4BNQMFDPEFJO(JU)VC

  6. 4BNQMF"QQSVOOJOHPO)FSPLV w IUUQQIQVOJUTFMFOJVNTBNQMFTIFSPLVBQQDPN

  7. 4UFQTPG5PEBZ w4UFQ w 4FUUJOH1)16OJU &YUFOTJPO w4UFQ w 4FUUJOH4FMFOJVN %SJWFS #SPXTFST

    w4UFQ w 8SJUFUFTUDPEF w.PSF w3FGFSFODFT
  8. 0WFSWJFX %FCJBO .BD049 8JOEPXT 1)16OJU &YUFOTJPO 4FMFOJVN4FSWFS %SJWFST #SPXTFST 4FMFOJVN4FSWFS

    %SJWFST #SPXTFST )FSPLV 8FC"QQ 172.16.25.135 172.16.25.131 172.16.25.132
  9. 4UFQ 4FUUJOH1)16OJU &YUFOTJPO

  10. 1)16OJU wY6OJUGPS1)1 w IUUQQIQVOJUEF $

  11. 1)16OJU&YUFOTJPO w0QUJPOBM1BDLBHFT w %C6OJU w GPS%BUBCBTFUFTU w 1)16OJU@4UPSZ w GPS4UPSZCBTFEUFTU

    w 1)16OJU@4FMFOJVN w GPS4FMFOJVNCBTFEUFTU
  12. 1)16OJU4FMFOJVN w1)16OJU@&YUFOTJPOT@4FMFOJVN5FTU$BTF w GPS4FMFOJVNY w 8FC%SJWFSDPNNBOE w1)16OJU@&YUFOTJPOT@4FMFOJVN5FTU$BTF w GPS4FMFOJVNY w

    4FMFOJVN3$DPNNBOE
  13. $PNQPTFS w%FQFOEFODZ .BOBHFSGPS1)1 w IUUQHFUDPNQPTFSPSH

  14. PO%FCJBO(/6-JOVY

  15. *OTUBMM w(FU$PNQPTFS • $ curl -s https://getcomposer.org/installer | php

  16. *OTUBMM w(FU1)16OJU &YUFOTJPO w DPNQPTFSKTPO w JOTUBMM • $ php

    composer.phar install { "require": { "phpunit/phpunit": "3.7.*", "phpunit/phpunit-selenium": ">=1.2" } }
  17. *OTUBMM w%JSFDUPSJFT vendor/ autoload.php bin/ phpunit (symlink) composer/ phpunit/ symfony/

    composer.json
  18. 4UFQ 4FUUJOH4FMFOJVN %SJWFS #SPXTFS

  19. 4FMFOJVN w#SPXTFSCBTF8FC"QQ5FTU5PPMT w 4FMFOJVN w 4FMFOJVN w IUUQTFMFOJVNIRPSH $

  20. PO.BD049 PO8JOEPXT

  21. *OTUBMM w%PXOMPBEl'JSFGPYz l$ISPNFz

  22. *OTUBMM w%PXOMPBEl+BWBz "UUFOUJPO,FFQVTJOH+BWBPGMBUFTUWFSTJPO

  23. *OTUBMM w%PXOMPBEl4FMFOJVN4FSWFSz

  24. *OTUBMM wPO.BD049 w %PXOMPBEl$ISPNF%SJWFSz w IUUQDPEFHPPHMFDPNQDISPNFESJWFSEPXOMPBETMJTU

  25. *OTUBMM wPO.BD049 w 4UPSFJOTBNFEJSFDUPSZ 4FMFOJVN4FSWFSBOE%SJWFS % ls -l chromedriver selenium-server-standalone-2.31.0.jar

  26. *OTUBMM wPO8JOEPXT w %PXOMPBEl*OUFSOFU&YQMPSFS%SJWFSz w IUUQEPDTTFMFOJVNIRPSHEPXOMPBE w %PXOMPBEl$ISPNF%SJWFSz w IUUQDPEFHPPHMFDPNQDISPNFESJWFSEPXOMPBETMJTU

  27. *OTUBMM wPO8JOEPXT w 4UPSFJOTBNFEJSFDUPSZ 4FMFOJVN4FSWFSBOE%SJWFS 2013/03/06 08:40 <DIR> . 2013/03/06

    08:40 <DIR> .. 2013/03/06 08:38 2,575,872 chromedriver.exe 2013/03/06 08:39 2,806,784 IEDriverServer.exe 2013/03/06 08:21 34,289,992 selenium-server-standalone-2.31.0.jar
  28. *OTUBMM wPO8JOEPXT *OUFSOFU&YQMPSFS 4FUUIF1SPUFDUFE.PEFTFUUJOHT GPSFBDI[POFUPCFUIFTBNFWBMVF

  29. *OTUBMM wPO8JOEPXT *OUFSOFU&YQMPSFS CSPXTFS[PPNMFWFMNVTUCFTFUUP GPSNPVTFFWFOU

  30. 'JSFGPY 4BGBSJ%SJWFS w/POFFEUPJOTUBMM w 4FMFOJVN4FSWFSCVOEMF'JSFGPYBOE4BGBSJESJWFS Selenium Server Firefox Driver IE

    Driver Chrome Driver Safari Driver 4BGBSJESJWFSPSVQQFSWFSTJPO
  31. #PPU4FMFOJVN4FSWFS w.BD049 w VTF5FSNJOBMBQQ w8JOEPXT w VTF$NEFYF $ java -jar

    selenium-server-standalone-2.31.0.jar > java -jar selenium-server-standalone-2.31.0.jar \ -Dwebdriver.ie.driver=./IEDriverServer.exe \ -Dwebdriver.chrome.driver=./chromedriver.exe lzJTMBUFTUWFSTJPOBUUIJTUJNF 4P JUXJMMDIBOHFBUUIFGVUVSF
  32. 8BSOJOH w4FMFOJVN4FSWFSEFGBVMU"MMPXGSPN"MM w 

  33. 4UFQ 8SJUFUFTUDPEF

  34. 5BSHFU8FC"QQ w'PSN http://phpunit-selenium2-samples.herokuapp.com/01/index.php

  35. 5BSHFU8FC"QQ w3FTVMU http://phpunit-selenium2-samples.herokuapp.com/01/receive.php

  36. 5BSHFU$PEF wJOEFYQIQ

  37. 5BSHFU$PEF wSFDFJWFQIQ

  38. 4UFQ 0QFOGPSN UJUMFDIFDL

  39. 4FU6Q UFTU5JUMF wTBNQMF5FTUQIQ

  40. &YUFOET w1)16OJU@&YUFOTJPOT@4FMFOJVN5FTU$BTF <?php class sample01Test extends PHPUnit_Extensions_Selenium2TestCase { } Not

    PHPUnit_Framework_TestCase
  41. TFU6Q w4FMFOJVN4FSWFS4FUUJOHT public function setUp() { $targetUrl = 'http://phpunit-selenium2-samples.herokuapp.com/01/'; $this->setHost('172.16.25.131');

    $this->setPort(4444); $this->setBrowser('firefox'); $this->setBrowserUrl($targetUrl); } 4FMFOJVN4FSWFS 5BSHFU#SPXTFS 5BSHFU63-
  42. UFTU5JUMF wUJUMFUFTU public function testTitle() { $this->url('/index.php'); $this->assertEquals('Sample 01', $this->title());

    } <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <link href="../css/bootstrap.min.css" rel="stylesheet"> <title>Sample 01</title> </head> JOEFYQIQ TBNQMF5FTUQIQ "TTFSUJPOCZ1)16OJU
  43. 4UFQ *OQVUGPSN BTTFUSFTVMU

  44. 4UFQ *OQVUGPSN

  45. UFTU'PSN w*OQVU'PSN

  46. UFTU'PSN OBNF <!-- Name --> <div class="control-group"> <label class="control-label" for="inputName">Name</label>

    <div class="controls"> <input type="text" name="data[name]" id="inputName" class="input-large"> </div> </div> // set Name $element = $this->byId('inputName'); // like getElementById() $element->value('suzuki'); JOEFYQIQ TBNQMF5FTUQIQ TFUWBMVF MJLFHFU&MFNFOU#Z*E
  47. UFTU'PSN FNBJM <!-- Email --> <div class="control-group"> <label class="control-label" for="inputEmail">Email</label>

    <div class="controls"> <input type="email" name="data[email]" id="inputEmail" class="input-large"> </div> </div> // set Email $this->byName('data[email]')->value('suzuki@example.com'); // method chaining JOEFYQIQ TBNQMF5FTUQIQ MJLFHFU&MFNFOU#Z/BNF
  48. UFTU'PSN GSVJUT <!-- Fruits --> <div class="control-group"> <label class="control-label" for="inputFruits">Which

    fruit do you like ?</label> <div class="controls"> <select name="data[fruits]" id="inputFruits"> <option value="apple">Apple</option> <option value="orange">Orange</option> <option value="grape">Grape</option> </select> </div> </div> // select Fruits $this->select($this->byId('inputFruits'))->selectOptionByLabel('Orange'); JOEFYQIQ TBNQMF5FTUQIQ same result: selectOptionByValue('orange') 4BGBSJEPFTOPUXPSLUIJT UBSHFUWBMVFPSBOHF -BCFM cc PQUJPO-"#&-PQUJPO
  49. UFTU'PSN DJUZ <!-- City --> <div class="control-group"> <label class="control-label">Do you

    want to go ?</label> <div class="controls"> <label class="checkbox"> <input type="checkbox" name="data[city][]" value="sapporo" id="inputCitySapporo">Sapporo </label> <label class="checkbox"> <input type="checkbox" name="data[city][]" value="tokyo" id="inputCityTokyo">Tokyo </label> <label class="checkbox"> <input type="checkbox" name="data[city][]" value="tokyo" id="inputCityOsaka">Osaka </label> </div> </div> // click City checkbox $this->byId('inputCitySapporo')->click(); JOEFYQIQ TBNQMF5FTUQIQ pSFDMJDLFWFOU
  50. UFTU'PSN OVNCFS <!-- Number --> <div class="control-group"> <label class="control-label">Which number

    like ?</label> <div class="controls"> <label class="radio"> <input type="radio" name="data[number]" id="inputNumberOdd" value="odd">Odd </label> <label class="radio"> <input type="radio" name="data[number]" id="inputNumberEven" value="even">Even </label> </div> </div> // click Number radio $this->byId('inputNumberEven')->click(); JOEFYQIQ TBNQMF5FTUQIQ pSFDMJDLFWFOU
  51. UFTU'PSN TVCNJU <!-- Button --> <div class="control-group"> <div class="controls"> <button

    type="submit" class="btn btn-primary" id="btnSubmit">Register</button> </div> </div> // submit form $this->byCssSelector('button.btn-primary')->click(); JOEFYQIQ TBNQMF5FTUQIQ $this->byCssSelector('button#btnSubmit')->click(); TBNFSFTVMUJOUIJTDBTF MJLF$44 CVUUPOCUOQSJNBSZ\ DPMPSSFE ^
  52. 4UFQ "TTFSUSFTVMU

  53. UFTU'PSN w"TTFSUSFTVMU

  54. UFTU'PSN BTTFSU <tr> <td>Name</td> <td><span id="resultName"><?php echo htmlspecialchars($results['name']); ?></span></td> </tr>

    <tr> <td>Email</td> <td><span id="resultEmail"><?php echo htmlspecialchars($results['email']); ?></span></td> </tr> <tr> <td>Fruits</td> <td><span id="resultFruits"><?php echo htmlspecialchars($results['fruits']); ?></span></td> </tr> // Name $element = $this->byId('resultName'); $innerText = $element->text(); $this->assertEquals('suzuki', $innerText); // Email $innerText = $this->byId('resultEmail')->text(); $this->assertEquals('suzuki@example.com', $innerText); // Fruits $this->assertEquals('orange', $this->byId('resultFruits')->text()); SFDFJWFQIQ TBNQMF5FTUQIQ
  55. UFTU'PSN BTTFSU <tr> <td>City</td> <td> <span id="resultCity"> <?php if (isset($results['city'])

    && is_array($results['city'])) { foreach ($results['city'] as $city) { $cities[] = htmlspecialchars($city); } echo implode(', ', $cities); } ?> </span> </td> </tr> <tr> <td>Number</td> <td><span id="resultNumber"><?php echo htmlspecialchars($results['number']); ?></span></td> </tr> // City $this->assertEquals('sapporo', $this->byId('resultCity')->text()); // Number $this->assertEquals('even', $this->byId('resultNumber')->text()); SFDFJWFQIQ TBNQMF5FTUQIQ
  56. .PSF

  57. 4FMFOJVN*%& w"VUPHFOFSBUF4FMFOJVN3$DPNNBOE

  58. 1)1'PSNBUUFS w1MVHJOGPS4FMFOJVN*%&

  59. *OTUBMM1)1'PSNBUUFS w1SPKFDUQBHF w IUUQTHJUIVCDPNTV[VLJ1)1'PSNBUUFS1)16OJU@4FMFOJVN w%PXOMPBEYQJpMF w IUUQCJUMZ;OKRE w IUUQTHJUIVCDPNTV[VLJ1)1'PSNBUUFS1)16OJU@4FMFOJVNCMPCNBTUFSCVJME QIQGPSNBUUFS![BUTVCVODPNYQJ

    SBXUSVF w%SBH%SPQUP'JSFGPY w pMFQIQGPSNBUUFS![BUTVCVODPNYQJESBHESPQ [BUTVCVODPNJTNZPXOEPNBJO
  60. 3FDPSEJOH

  61. &YQPSU w&YQPSU1)16OJU@4FMFOJVN5FTU$BTFDPEF

  62. "VUPHFOFSBUF

  63. 3FGFSFODFT

  64. 1)16OJUEPDVNFOU w IUUQXXXQIQVOJUEFNBOVBMKBTFMFOJVNIUNMTFMFOJVNTFMFOJVNUFTUDBTF

  65. 4BNQMFJO(JU)VC w IUUQTHJUIVCDPNTFCBTUJBOCFSHNBOOQIQVOJUTFMFOJVNCMPCNBTUFS5FTUT 4FMFOJVN5FTU$BTF5FTUQIQ

  66. 4FMFOJVN8JLJ w IUUQDPEFHPPHMFDPNQTFMFOJVNXMJTU

  67. 5IF8FC%SJWFS8JSF1SPUDPM w IUUQDPEFHPPHMFDPNQTFMFOJVNXJLJ+TPO8JSF1SPUPDPM

  68. .ZXPSLJOHEPDVNFOU w IUUQCJUMZQIQVOJUTFMFOJVN

  69. 2VFTUJPO

  70. 5IBOLT