Phpday - Automated acceptance testing with Behat and Mink

6,294 views

Published on

Published in: Education, Technology

Phpday - Automated acceptance testing with Behat and Mink

  1. 1. PHPDAY 2013, VERONAAutomated acceptance testing with Behat and Mink&Saturday, May 18, 13
  2. 2. LET ME INTRODUCE MYSELF• @Richard_Tuin• Software developer from the Netherlands• Particular interest in QA• Working forSaturday, May 18, 13
  3. 3. INEFFICIENCY• Different view on scope and resulting product• Clients don’t get a feel of how they will use the product• “I like what you’ve built, but it doesn’t do what i meant”• “Thanks for this new feature, but now x stopped working!”Saturday, May 18, 13
  4. 4. INEFFICIENCY• Different view on scope and resulting product• Clients don’t get a feel of how they will use the product• “I like what you’ve built, but it doesn’t do what i meant”• “Thanks for this new feature, but now x stopped working!”Saturday, May 18, 13
  5. 5. BECAUSE• ... the dev team and the client have the same goal• ... we want to know that what we build has value• ... we want to deliver consistent qualitySaturday, May 18, 13
  6. 6. BUTTHERE’S ANOTHERPROBLEM...Saturday, May 18, 13
  7. 7. Developers are lazySaturday, May 18, 13
  8. 8. SOTHIS MEANS...• ... we have to focus more on communication• ... leave no room for misinterpretation• ... we have to work together with the client• ... we got to have a system• ... we have to validate regularly if what we have built is stillaccording to acceptance criteriaSaturday, May 18, 13
  9. 9. SOTHIS MEANS...• ... we have to focus more on communication• ... leave no room for misinterpretation• ... we have to work together with the client• ... we got to have a system• ... we have to validate regularly if what we have built is stillaccording to acceptance criteriaAutomation?Saturday, May 18, 13
  10. 10. WORKTOGETHER & DOCUMENTTOGETHERSaturday, May 18, 13
  11. 11. AUTOMATETHEVALIDATIONAGAINST ACCEPTANCECRITERIADocumentationSaturday, May 18, 13
  12. 12. SO... HOW?Saturday, May 18, 13
  13. 13. ITERATIVEis the keywordSaturday, May 18, 13
  14. 14. ITERATIVE“Gradually get to the highest value.”Saturday, May 18, 13
  15. 15. ITERATIVE SOFTWAREDEVELOPMENTSaturday, May 18, 13
  16. 16. WRITING REQUIREMENTS• Describe how a problem will be solved• Write them together with your client• In the (technical) language of the client• Try to describe the functional problem, not the technical oneSaturday, May 18, 13
  17. 17. WRITE EXAMPLES!They will reduce misinterpretation and illustrate the resultSaturday, May 18, 13
  18. 18. FEATURE FILE LAYOUTFeature: {feature description}{intention}As a {person}I want {feature}So that {intention}Scenario: {specific scenario description}Given {context}And {more context}When {action}Then {result}Scenario: ...Information: http://dannorth.net/whats-in-a-story/Saturday, May 18, 13
  19. 19. EXAMPLE FEATUREFeature: Searching on the internetAs a google.com visitorI want to use the search engineSo that i can find information on the internetScenario: Simple keyword searchGiven I am on the homepageWhen I fill in the search term “PHP”And I press “Google search”Then I should see search results containing “PHP”Scenario: I feel luckyGiven I am on the homepageWhen I fill in the search term “PHP”And I press “I’m feeling lucky”Then I should not be on google.comAnd I should see “PHP”Saturday, May 18, 13
  20. 20. STEPS? (TERMINOLOGY)Feature: {feature description}{intention}As a {person}I want {feature}So that {intention}Scenario: {specific scenario description}Given {context}And {more context}When {action}Then {result}Scenario: ...Saturday, May 18, 13
  21. 21. STEPS? (TERMINOLOGY)Feature: {feature description}{intention}As a {person}I want {feature}So that {intention}Scenario: {specific scenario description}Given {context}And {more context}When {action}Then {result}Scenario: ...Feature, user story, moduleSaturday, May 18, 13
  22. 22. STEPS? (TERMINOLOGY)Feature: {feature description}{intention}As a {person}I want {feature}So that {intention}Scenario: {specific scenario description}Given {context}And {more context}When {action}Then {result}Scenario: ...Feature, user story, moduleScenarioSaturday, May 18, 13
  23. 23. STEPS? (TERMINOLOGY)Feature: {feature description}{intention}As a {person}I want {feature}So that {intention}Scenario: {specific scenario description}Given {context}And {more context}When {action}Then {result}Scenario: ...Feature, user story, moduleScenarioStepsSaturday, May 18, 13
  24. 24. LIVING DOCUMENTATION• All features and scenarios make great documentation!• A change request starts with rewriting documentation• Easy to see what functionality the application contains• Easy to get a feel for the impact of a change• Easy to get a new team member involvedSaturday, May 18, 13
  25. 25. COMMUNICATION IS KEYAutomation is not the focusSaturday, May 18, 13
  26. 26. Developers are lazySaturday, May 18, 13
  27. 27. AUTOMATIONMakes us happy :-)Saturday, May 18, 13
  28. 28. “A php framework for testing your business expectations.”-behat.orgSaturday, May 18, 13
  29. 29. “Mink is an open source acceptance test framework for webapplications, written in PHP 5.3.”Saturday, May 18, 13
  30. 30. +Saturday, May 18, 13
  31. 31. +AWESOMESaturday, May 18, 13
  32. 32. COMPONENTS OF A BEHAT+MINKFUNCTIONALTEST SUITE• Features• Feature parser• Generic browser controller client• Browser controller/simulator• Executable code that controls the browser controller clientSaturday, May 18, 13
  33. 33. COMPONENTS OF A BEHAT+MINKFUNCTIONALTEST SUITE• Features• Feature parser• Generic browser controller client• Browser controller/simulator• Executable code that controls the browser controller clientLiving documentationSaturday, May 18, 13
  34. 34. COMPONENTS OF A BEHAT+MINKFUNCTIONALTEST SUITE• Features• Feature parser• Generic browser controller client• Browser controller/simulator• Executable code that controls the browser controller clientLiving documentationBehatSaturday, May 18, 13
  35. 35. COMPONENTS OF A BEHAT+MINKFUNCTIONALTEST SUITE• Features• Feature parser• Generic browser controller client• Browser controller/simulator• Executable code that controls the browser controller clientLiving documentationBehatMinkSaturday, May 18, 13
  36. 36. COMPONENTS OF A BEHAT+MINKFUNCTIONALTEST SUITE• Features• Feature parser• Generic browser controller client• Browser controller/simulator• Executable code that controls the browser controller clientLiving documentationBehatMinkSelenium, Zombie.js, Goutte,etc.Saturday, May 18, 13
  37. 37. COMPONENTS OF A BEHAT+MINKFUNCTIONALTEST SUITE• Features• Feature parser• Generic browser controller client• Browser controller/simulator• Executable code that controls the browser controller clientLiving documentationBehatMinkSelenium, Zombie.js, Goutte,etc.MinkExtensionSaturday, May 18, 13
  38. 38. 1. Using Composer or Phar$ composer install{"require": {"behat/behat": "*","behat/mink": "*","behat/mink-extension": "*","behat/mink-goutte-driver": "*","behat/mink-selenium2-driver": "*"},"minimum-stability": "dev","config": {"bin-dir": "bin/"}}INSTALLING BEHAT+MINKSaturday, May 18, 13
  39. 39. 1. Using Composer or Phar$ composer install{"require": {"behat/behat": "*","behat/mink": "*","behat/mink-extension": "*","behat/mink-goutte-driver": "*","behat/mink-selenium2-driver": "*"},"minimum-stability": "dev","config": {"bin-dir": "bin/"}}INSTALLING BEHAT+MINKThat’s it!Saturday, May 18, 13
  40. 40. HELLO BEHAT1. Initialize a Behat testsuite skeleton$ bin/behat --init2. Behat creates it’s standard project structureSaturday, May 18, 13
  41. 41. MINKEXTENSION1. Bridges the gap between Behat and Mink2. Contains a set of predefined step definitions3. Not all step definitions may be usable for all projects4. Behat + Mink is easy:use BehatMinkExtensionContextMinkContext;class FeatureContext extends MinkContext{// ...}features/FeatureContext.phpSaturday, May 18, 13
  42. 42. MINKEXTENSION STEPSGiven /^(?:|I )am on (?:|the )homepage$/When /^(?:|I )go to (?:|the )homepage$/Given /^(?:|I )am on "(?P<page>[^"]+)"$/When /^(?:|I )fill in "(?P<field>(?:[^"]|")*)" with "(?P<value>(?:[^"]|")*)"$/When /^(?:|I )press "(?P<button>(?:[^"]|")*)"$/When /^(?:|I )follow "(?P<link>(?:[^"]|")*)"$/Then /^(?:|I )should be on "(?P<page>[^"]+)"$/Then /^(?:|I )should see "(?P<text>(?:[^"]|")*)"$/$ bin/behat -dl/*** Clicks link with specified id|title|alt|text.** @When /^(?:|I )follow "(?P<link>(?:[^"]|")*)"$/*/public function clickLink($link){$link = $this->fixStepArgument($link);$this->getSession()->getPage()->clickLink($link);}Saturday, May 18, 13
  43. 43. BEHAT.YML• Place for configuration options• Basic Behat / Mink configdefault:extensions:BehatMinkExtensionExtension:base_url: "http://www.yourwebsite.com"goutte: ~selenium2: ~Saturday, May 18, 13
  44. 44. EXAMPLE FEATURESaturday, May 18, 13
  45. 45. EXAMPLE FEATUREFeature: Search on the internetAs a google.com visitorI want to use the search engineSo that i can find information on the internetScenario: Simple keyword searchGiven I am on the homepageWhen I fill in the search term “PHP”And I press “Google search”Then I should see search results containing “PHP”features/search.featureSaturday, May 18, 13
  46. 46. STEP DEFINITIONS/*** @When /^I fill in the search term "([^"]*)"$/*/public function iSearchTheTerm($arg1){throw new PendingException();}Put these in your FeatureContext.php file.$ bin/behatRunning BehatBehat gives you step suggestions:Saturday, May 18, 13
  47. 47. WRITING STEP DEFINITIONS/*** @When /^I search the term "([^"]*)"$/*/public function iSearchTheTerm($searchTerm){$this->fillField(q, $searchTerm); // method from MinkExtension$this->pressButton(Google search);}Saturday, May 18, 13
  48. 48. MINKTERMINOLOGY• Driver = Browser controller/emulator• Session = Browser• Page = Document(Element)• Element• Selectors• XPath• CSS• NamedSaturday, May 18, 13
  49. 49. RUNNING ITSaturday, May 18, 13
  50. 50. PAGE/DOCUMENTTRAVERSAL• Selection of elements on a page• Selectors based on XPath, CSS, Names$page = $this->getPage();$elements = $page->findAll(xpath, .//div[@class="sa_mc"]);$elements = $page->findAll(css, div[class="sa_mc"]);Saturday, May 18, 13
  51. 51. ELEMENT ACTIONS$page = $this->getSession()->getPage();$loginLink = $page->find(css, #login);$loginLink->click();->getValue();->setValue();->keyUp();->press();etcetera...Saturday, May 18, 13
  52. 52. OTHER FEATURESGreat to get startedSaturday, May 18, 13
  53. 53. PROFILES• Full configuration per environment• Feature / Scenario selection based on filters• Define them in behat.yml:• Call the profile with: $ bin/behat --profile acceptancedefault:extension:BehatMinkExtensionContextMinkContextbase_url: “http://www.example.org”acceptance:extension:BehatMinkExtensionContextMinkContextbase_url: “http://acc.example.org”Saturday, May 18, 13
  54. 54. TAGS• Metadata for features and scenarios• Via command line: $ bin/behat --tags “@smoke”• Define as filter in profile@smokeFeature: Search on the www@javascriptScenario: ...search.featuresmoke:filters:tags: “@smoke”behat.ymlnojs:filters:tags: “~@javascript”behat.ymlSaturday, May 18, 13
  55. 55. FILTERS• Grouping tests• Tags• Configuration in behat.ymlsmoketests:filters:tags: “@smoketest&&~@wip”development:filters:tags: “~@slow&&~@wip”Saturday, May 18, 13
  56. 56. BROWSER CONTROLLERSJavascript Speed RemarkGoutte No ++ EmulatorSelenium2 Yes -Zombie.js Yes +Saturday, May 18, 13
  57. 57. SCENARIO OUTLINESScenario Outline: Simple keyword searchGiven I am on the homepageWhen I search the term <searchterm>Then I should see search results containing <searchterm>Examples:| searchterm || PHP || Java || Pie || This string is possibly too long and uncommon |Saturday, May 18, 13
  58. 58. DRIVER BENCHMARKGoutte 2.736msSelenium2 16.682msZombie.js 7.533ms0 5,000 10,000 15,000 20,000Goutte Selenium2 Zombie.jsSaturday, May 18, 13
  59. 59. HOOKS• @beforeSuite• @beforeFeature• @beforeScenario• @beforeStep• @afterSuite• @afterFeature• @afterScenario• @afterStepSaturday, May 18, 13
  60. 60. HOOKS• @beforeSuite• @beforeFeature• @beforeScenario• @beforeStep• @afterSuite• @afterFeature• @afterScenario• @afterStepSaturday, May 18, 13
  61. 61. HOOK EXAMPLE/*** @afterScenario*/public function logoutUser(){$this->visit(/logout);}features/bootstrap/FeatureContext.phpSaturday, May 18, 13
  62. 62. LOCALIZATION• Write features in your own language# language: itFunzionalità: non so scrivere italianoScenario: ...Dato ...Quando ...Allora ...Schema dello scenario: ...Dato ...Quando ...Allora ...Esempi:...Saturday, May 18, 13
  63. 63. INTEGRATION WITH JENKINS• Behat includes export to jUnit format• Jenkins build step example${WORKSPACE}/bin/behat --format junit --out ${WORKSPACE}/buildSaturday, May 18, 13
  64. 64. CONCLUSION1. Writing examples is great to improve communication2. Automating acceptance criteria = consistent quality3. Behat and Mink are an excellent combination to do thisSaturday, May 18, 13
  65. 65. QUESTIONS?Saturday, May 18, 13
  66. 66. THANKYOU!• Feedback: http://joind.in/talk/view/8647• More questions? Contact me, i love this subject!• @Richard_Tuin• richardtuin@gmail.com• skype: richardtuinSaturday, May 18, 13

×