Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Behat bdd training (php) course slides pdf

4,569 views

Published on

behat, mink, bdd, gherkin , php training , behaviour driven development, london, uk,

Published in: Education, Technology
  • Be the first to comment

Behat bdd training (php) course slides pdf

  1. 1. Behat and BDD web training (PHP) 2 days
  2. 2. Who is the training for? • Ideal for testers wanting to gain BDD and automation testing experience • Some programming knowledge required but not essential • Testers involved with website testing • Testers wanting to gain technical skills • PHP programmers and Drupal programmers 2 www.time2test.co.uk
  3. 3. High level • BDD overview • Step Definitions • Behat History • Context Class • PHP Versions • Assertions • Installation • Command Line • Quick Usage Reference • Gherkin Language • Features • Mink Web Testing 3 www.time2test.co.uk
  4. 4. What will you learn? • Behaviour Driven Development Overview • Behat Overview and configuration • Gherkin Language Explained • Syntax - Givens, Whens, Thens, And, But • Test Data • 4 Command Line usage • Understand Features, Scenarios, Step Definitions Context Class and Assertions • End to End Behat examples • • Mink for web testing www.time2test.co.uk
  5. 5. schedule Day 1 Day 2 • php primer • gherkin • behat background • case studies • mink api 5 www.time2test.co.uk
  6. 6. PHP Primer
  7. 7. overview Lets have a quick PHP programming recap 7 www.time2test.co.uk
  8. 8. syntax PHP tags <?php … ?> ! Comments with # 8 www.time2test.co.uk
  9. 9. variables • All variables lead with $ Integers $int_var = 12345; • assignments with = operator Doubles $doubles = 3.42 • • Boolean TRUE or FALSE constants variables don’t need to be declared Null $my_var = NULL; no need to specify the type e.g. String or int Strings $string = “This is some text”; 9 www.time2test.co.uk
  10. 10. Constants • identifier that can not change • UPPERCASE • define(“NAME”, value); • echo constant(“NAME”); • Magic constants - ___LINE___, ___FILE___ e.t.c 10 www.time2test.co.uk
  11. 11. Operators • Arithmetic Operators ( +, -, *, %, ++, —) • Comparison Operators ( ==, !=, >, <, >=, <=) • Logical operators ( and, or, &&, ||, !) • Assignment operators ( =, +=, -=, *=, /=, %= 11 www.time2test.co.uk
  12. 12. Decisions • If… Else • elseIf • Switch 12 www.time2test.co.uk
  13. 13. Loops for (initial; condition; increment) { code to be executed; } while ( condition) { code to be executed; } do { code to be executed; } while ( condition); foreach (array as value) { code to be exe; } break continue 13 www.time2test.co.uk
  14. 14. arrays • numeric arrays - numeric index • associative array - strings used as index • multidimensional array - array of arrays 14 www.time2test.co.uk
  15. 15. strings • single quotes versus double quotes • string concatenation with a . • strlen($mystring) function • strpos($mystring, $searchstring) function 15 www.time2test.co.uk
  16. 16. File Input/Output • fopen() - open a file • filesize() - file length • fread() - read a file • fclose() - close a file 16 www.time2test.co.uk
  17. 17. functions • functions • functions with parameters • functions with return values • functions with default parameters 17 www.time2test.co.uk
  18. 18. regular expressions • are a sequence of pattern of characters • provide pattern matching • POSIX and PERL style matching 18 www.time2test.co.uk
  19. 19. Exceptions Handling • try • throw • catch 19 www.time2test.co.uk
  20. 20. Debugging • missing semicolons • not enough equal signs • misspelled variable names • missing dollar signs • troubling quotes • missing parentheses and curly brackets • array index 20 www.time2test.co.uk
  21. 21. date • time() - returns seconds from 1 jan 1970 • getdate() - returns an associative array • date() - returns formatted string 21 www.time2test.co.uk
  22. 22. Object Orientated Programming • class • object • member variables • member functions • inheritance • constructors 22 www.time2test.co.uk
  23. 23. class - $this • $this - is a special variable and it refers to the same object i.e itself 23 www.time2test.co.uk
  24. 24. objects • using new keyword • $travel = new Books; • call member functions • $travel->setTitle(“travel to london”); • $travel->setPrice(100); 24 www.time2test.co.uk
  25. 25. constructors • special functions which are automatically called whenever an object is created/ initialised • __construct() to define a constructor with arguments 25 www.time2test.co.uk
  26. 26. public, private and protected members • public members are accessible insside, outside and in another the class to which is declared • private members are limited to the class its declared • protected members are limited to the class its declared and extended classes only 26 www.time2test.co.uk
  27. 27. interfaces • common function names to implementors who then develop code 27 www.time2test.co.uk
  28. 28. constants • declared constants can not change 28 www.time2test.co.uk
  29. 29. abstract classes • abstract classes can not be instantiated , only inherited 29 www.time2test.co.uk
  30. 30. static • static members or methods are accessible without needing an instantiation of the class 30 www.time2test.co.uk
  31. 31. final • final methods can not be overdided by child classes 31 www.time2test.co.uk
  32. 32. parent constructors • sub classes can extend the parent constructors. 32 www.time2test.co.uk
  33. 33. Environment
  34. 34. overview • Mac • Windows • Unix • PHP 5 installed • PHP IDE 34 www.time2test.co.uk
  35. 35. PHP IDEs • Integrated Development environments • many available -free and paid • Windows or Mac or Linux 35 www.time2test.co.uk
  36. 36. Background
  37. 37. What is BDD? • human readable stories • define business outcomes and drill into features • testing framework • Extends TDD - test driven development • Write a test that fails then watch it pass 37 www.time2test.co.uk
  38. 38. Why BDD? • Deliver what you client wants • Better communications and better collaboration • Extends the principles of TDD ( Test Data Driven) testing. 38 www.time2test.co.uk
  39. 39. Behat History • Behat is the PHP version of Cucumber • created by Konstantin Kudryashov (@everzet) 39 www.time2test.co.uk
  40. 40. What does Behat do? Scenario Step Given I have a file named “foo” regex Given /^I have a file named“([^”$/ Definition public function iHaveAFileNamed($file{ Do some work touch($file) Pass and Fal at each step unless an exception is thrown 40 www.time2test.co.uk
  41. 41. Good BDD • practice • get into the zone for creating features and scenarios • for web testing - understand the mink api 41 www.time2test.co.uk
  42. 42. Quick Overview
  43. 43. overview • lets jump straight into an end to end example using Behat and Mink 43 www.time2test.co.uk
  44. 44. composer • dependency manager for PHP external libraries 44 www.time2test.co.uk
  45. 45. dependencies • download the dependencies using a composer.json file • $php composer.phar install 45 www.time2test.co.uk
  46. 46. behat —help • $>php bin/behat —help 46 www.time2test.co.uk
  47. 47. behat.yml • create this file at root level • settings file • pronounce ( ya-mul ?) 47 www.time2test.co.uk
  48. 48. initialise project • $>php bin/behat —init • This will create some directories • +d features - place your *.feature files here • +d features/bootstrap - place bootstrap scripts and static files here • Also will create the file FeatureContext.php • +f features/bootstrap/FeatureContext.php - place your feature related code here 48 www.time2test.co.uk
  49. 49. FeatureContext.php • Extend context from MinkContext instead of the BehatContext 49 www.time2test.co.uk
  50. 50. Feature Feature: Search In order to find an article As a website user I need to be able to search for am article 50 www.time2test.co.uk
  51. 51. Execute Feature ! $bin/behat features/search.feature: • Magic happens and test results shown • Good for headless browser without javascript 51 www.time2test.co.uk
  52. 52. Selenium • Download selenium server from seleniumhq • run selenium standalone server • java -jar selenium-server-standalone-2.38.0.jar • Tag your scenario with @javascript annotation 52 www.time2test.co.uk
  53. 53. javascript test Run in a browser that supports javascript @javascript 53 www.time2test.co.uk
  54. 54. javascript execute ! • $bin/behat features/search.feature: • The Firefox browser is invoked and the test execution is visible 54 www.time2test.co.uk
  55. 55. implement a new step execution will conveniently come back with a list of undefined steps • /** * @Given /^I wait for (d+) seconds$/ */ public function iWaitForSeconds($arg1) { throw new PendingException(); } 55 www.time2test.co.uk
  56. 56. new step definition code // copy & paste at features/bootstrap/FeatureContext.php ! /** * @Given /^I wait for (d+) seconds$/ */ public function iWaitForSeconds($seconds) { $this->getSession()->wait($seconds*1000); } 56 www.time2test.co.uk
  57. 57. Configuration • behat.yml 57 www.time2test.co.uk
  58. 58. Define a Feature • 4 line description • a line describes the feature • three following lines describe the benefit, the role and feature itself 58 www.time2test.co.uk
  59. 59. Define a Scenario • many scenarios per feature • three line syntax • Given - describe the initial state • When - action the user takes • And Then - what the user sees after the action 59 www.time2test.co.uk
  60. 60. Keywords • Given • And • When • Then 60 www.time2test.co.uk
  61. 61. Execution • $> behat 61 www.time2test.co.uk
  62. 62. Writing Step Definitions • behat matches each statement of a scenario to a list of regular expression steps • Behat helps by creating an outline for undefined steps 62 www.time2test.co.uk
  63. 63. Regular expressions • Behat and Mink rely on regex • here is a quick masterclass on regex 63 www.time2test.co.uk
  64. 64. multi lines • triple quotation syntax (“””) 64 www.time2test.co.uk
  65. 65. Directory Structure • behat —init • features • features/bootstrap/ *.php files • features/bootstrap/featureContext.php 65 www.time2test.co.uk
  66. 66. Further Details
  67. 67. Features Explained • features are in a format called Gherkin • each feature file consists of a single feature • each feature will consist of a list of scenarios 67 www.time2test.co.uk
  68. 68. Step Definitions • written in php • consists of a keyword, regular expression and a callback • the regex is a method argument 68 www.time2test.co.uk
  69. 69. Context Class • behat creates a context object for each scenario 69 www.time2test.co.uk
  70. 70. Assertions with PHPUnit // require_once 'PHPUnit/Autoload.php'; require_once 'PHPUnit/Framework/Assert/ Functions.php'; // • assertEquals($numberOfRows, count($elements)); 70 www.time2test.co.uk
  71. 71. Command Line Usage >>behat —h - help options >>behat —v version >>behat —dl - view the step definitions >>behat —init - setup a features dir and featurecontext.php file 71 www.time2test.co.uk
  72. 72. Gherkin Language
  73. 73. Overview • common language for behaviour driven testing 73 www.time2test.co.uk
  74. 74. Syntax • line orientated language • parser divides input into features, scenarios and steps. 74 www.time2test.co.uk
  75. 75. Features • each feature file consist of a single feature • keyword feature: and three indented lines 75 www.time2test.co.uk
  76. 76. Scenarios • each scenario begins with keyword Scenario: followed by an optional scenario title 76 www.time2test.co.uk
  77. 77. scenario outlines • template placeholders for easy multi input 77 www.time2test.co.uk
  78. 78. backgrounds • run before each scenario • background: kewword 78 www.time2test.co.uk
  79. 79. Steps • features consist of steps: given, when, then 79 www.time2test.co.uk
  80. 80. Given • known state before the user or system interacts with the application under test 80 www.time2test.co.uk
  81. 81. When • user action performed on the system under test 81 www.time2test.co.uk
  82. 82. Then • observe the outcome and related to the feature description 82 www.time2test.co.uk
  83. 83. And, But • Use and and but for elegant and natural reading • not needed but good practice 83 www.time2test.co.uk
  84. 84. Test Data • Tables used for injecting data - different from scenario outlines • PyStrings - used for larger text input across many lines 84 www.time2test.co.uk
  85. 85. tags • used to organise features and scenarios 85 www.time2test.co.uk
  86. 86. Mink - Web Testing
  87. 87. Overview • Mink is an Extension to Behat • Think of a plugin 87 www.time2test.co.uk
  88. 88. What is Mink? • Standalone library to use PHP to command a browser • API to send commands to Selenium, Goutte, ZombieJS and more • The extension allows for BDD tests to be created without actually writing any PHP code 88 www.time2test.co.uk
  89. 89. Installation • Update composer.json to include • Mink • MinkExtension • Goutter adn Selenium2 drivers for Mink • Then update the vendor libraries ising • $>php composer.phar update 89 www.time2test.co.uk
  90. 90. MinkExtension • behat.yml is the behat config file • http://docs.behat.org/guides/7.config.html 90 www.time2test.co.uk
  91. 91. MinkContext Extension • Gives us access to the Mink Session to allow us to send commands to a browser • Inheritance of a pre-existing definitions • Before extending : we have 4 definitions • After extending : there are lots of definitions • Try it: $>php bin/behat -dl 91 www.time2test.co.uk
  92. 92. wikipedia example • lets look at a wikipedia example 92 www.time2test.co.uk
  93. 93. First Example - Mink headless lets look at a headless mink example 93 www.time2test.co.uk
  94. 94. example - Mink with Selenium • lets look at a javascript browser example 94 www.time2test.co.uk
  95. 95. Mink api
  96. 96. Overview • http://mink.behat.org/api/index.html • Lets look at some common Mink API 96 www.time2test.co.uk
  97. 97. visit() • $this->visit('/'); 97 www.time2test.co.uk
  98. 98. fillField • fillField('email', $email); 98 www.time2test.co.uk
  99. 99. pressButton() • pressButton('Login'); 99 www.time2test.co.uk
  100. 100. getSession() • $this->getSession(); 100 www.time2test.co.uk
  101. 101. getPage() • getPage(); 101 www.time2test.co.uk
  102. 102. findAll • css elements example • findAll(‘css’,’css_value_goes_here’); 102 www.time2test.co.uk
  103. 103. findButton() • findButton(‘html_link_name’); 103 www.time2test.co.uk
  104. 104. findButton()->click() • having found the button element , you wish to click • findButton(‘html-link’)->click(); 104 www.time2test.co.uk
  105. 105. locatePath() • based on current session, now goto the defined path • visit($this->locatePath(‘/user’)); 105 www.time2test.co.uk
  106. 106. getStatusCode() • return the HTTP status code from the current session • $session->getStatusCode(); 106 www.time2test.co.uk
  107. 107. getCurrentUrl() • ->getCurrentUrl(); 107 www.time2test.co.uk
  108. 108. Next Steps
  109. 109. Practice • Try out the different mink web emulators • Sublime extension • phantom.js • Symfony • Drupal extension 109 www.time2test.co.uk
  110. 110. case studies
  111. 111. Wordpress casestudy • Wordpress Behat Mink Context Example 111 www.time2test.co.uk
  112. 112. case study 2 • Lets look at an example - behat with mink 112 www.time2test.co.uk
  113. 113. case study 3 • Lets look at an example - behat ,mink , website 113 www.time2test.co.uk
  114. 114. case study 4 • google search 114 www.time2test.co.uk
  115. 115. case study 5 115 www.time2test.co.uk
  116. 116. case study 6 • features master class 116 www.time2test.co.uk
  117. 117. case study 7 • ls example on unix 117 www.time2test.co.uk
  118. 118. case study 8 • open scholar project • good established behat mink framework 118 www.time2test.co.uk
  119. 119. case study 9 • Behat + Mink demo github 119 www.time2test.co.uk
  120. 120. Conclusions
  121. 121. goals and objectives • Review your goals. • Have we met your expectations? • Email us with your feedback 121 www.time2test.co.uk
  122. 122. Thank you • From the Time2test Team 122 www.time2test.co.uk

×