Upgrade to Pro — share decks privately, control downloads, hide ads and more …

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

Norio Suzuki

March 09, 2013
Tweet

More Decks by Norio Suzuki

Other Decks in Technology

Transcript

  1. 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
  2. *OTUBMM w(FU1)16OJU &YUFOTJPO w DPNQPTFSKTPO w JOTUBMM • $ php

    composer.phar install { "require": { "phpunit/phpunit": "3.7.*", "phpunit/phpunit-selenium": ">=1.2" } }
  3. *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
  4. #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
  5. 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-
  6. 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
  7. 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
  8. 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('[email protected]'); // method chaining JOEFYQIQ TBNQMF5FTUQIQ MJLFHFU&MFNFOU#Z/BNF
  9. 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
  10. 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
  11. 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
  12. 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 ^
  13. 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('[email protected]', $innerText); // Fruits $this->assertEquals('orange', $this->byId('resultFruits')->text()); SFDFJWFQIQ TBNQMF5FTUQIQ
  14. 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