ATDD with Behat and Selenium (LDNSE6)

6,370 views

Published on

These are the slides of the talk given at London Selenium Meetup on 29 may 2012. This talk was on Behat, MinkExtension and Selenium. http://www.meetup.com/seleniumlondon/events/61732192/
Pic : http://www.meetup.com/seleniumlondon/photos/8638912/#124419342

Published in: Technology

ATDD with Behat and Selenium (LDNSE6)

  1. 1. ATDD with Behat and Selenium
  2. 2. About MeShashikant Jagtap [Shashi]Twitter: @Shashikant86London Behat Users [#LDNBU]BDD Enthusiastic Tester
  3. 3. OverviewATDD
  4. 4. OverviewATDDBehat from Scratch
  5. 5. OverviewATDDBehat from ScratchComposer & Mink Extension
  6. 6. OverviewATDDBehat from ScratchComposer & MinkExtensionSpeeding up BDD with Sublime Text2Browser Automation with SeleniumATDD with Behat and SeleniumANT & HTML Report GenerationVideo/Live Demo
  7. 7. ATDDAcceptance Test Driven Development Discuss Stor Stor y2 Stor y3 y1
  8. 8. ATDDAcceptance Test Driven Development Specification Workshop Discuss Distill Stor Stor y2 Stor y3 Tester Develope y1 PM/S rs M
  9. 9. ATDDAcceptance Test Driven Development Specification Workshop Discuss Distill Stor Stor y2 Stor y3 Tester PM/S Develope y1 M rs Acceptance Tests Implement Test Devel er oper
  10. 10. ATDD Acceptance Test Driven Development Specification Workshop Discuss Distill Stor Stor y2 Stor y3 Tester PM/S Develope y1 M rs Acceptance Tests Executable Spec ImplementAutomat Demo ion Test Automat Devel er ion oper
  11. 11. ATDDGet Acceptance Criteria
  12. 12. ATDDGet Acceptance CriteriaWrite Acceptance Test Collaboratively [Failing/Undefined]
  13. 13. ATDDGet Acceptance CriteriaWrite Acceptance Test Collaboratively [Failing/Undefined]Implement Step Definition to make it Pass
  14. 14. ATDDGet Acceptance CriteriaWrite Acceptance Test Collaboratively [Failing/Undefined]Implement Step Definition to make it PassTest Pass !! Automation Achieved.
  15. 15. BehatIts cousin of
  16. 16. BehatBehat is a BDD tool for PHP Applications http://behat.org/Mink is a web Acceptance Testing Framework for PHP http://mink.behat.org/Combination of Behat/Mink can be used for web acceptance Testing.Mink has Selenium1 & Selenium2 Drivers for browser automation
  17. 17. Who is Doing/Maintaining?Konstantin Kudryashov - Knplabs, FranceTwitter : @everzetGitHub : Everzet+ Some Awesome Contributors
  18. 18. Lets Get Started With Behat Pre-requisiteLaptop/ComputerPHP Stacks (LAMP, WAMP, MAMP)Selenium ServerCurl or wgetGitHub (optional)
  19. 19. Behat Installation
  20. 20. Could you Live Without
  21. 21. If Not Then
  22. 22. If Not Then$ pear channel-discover pear.symfony.com$ pear channel-discover pear.behat.org$ pear install behat/behatpear install behat/mink
  23. 23. If Not Then$ pear channel-discover pear.symfony.com$ pear channel-discover pear.behat.org$ pear install behat/behatpear install behat/minkYou Ready to use Behat/Mink,
  24. 24. If Not Then$ pear channel-discover pear.symfony.com$ pear channel-discover pear.behat.org$ pear install behat/behatpear install behat/minkYou Ready to use Behat/Mink,WAIT.... We got EASIER OPTION !!!!!
  25. 25. We Got Composer !!!!!!
  26. 26. We Got Composer !!!!!!Composer is dependency manager for PHPComposer allow you to declare libraries you needed and install it for youJust define all dependencies in composer.json file and you are done !!So, Lets do that
  27. 27. Create Project Make New Directory$ mkdir BehatDemo$ cd BehatDemo
  28. 28. Create composer.json{"require": { "behat/behat": "2.4@stable", "behat/mink-extension": "dev-master", "fabpot/goutte": "*", "alexandresalome/php-selenium": "*", "instaclick/php-webdriver": "*" }, "config": { "bin-dir": "bin" }}
  29. 29. Create behat.ymldefault:context: class: FeatureContext extensions: BehatMinkExtensionExtension: base_url: http://saucelabs.com/ javascript_session: selenium2 goutte: ~ selenium2:
  30. 30. Download ComposerDownload composer using command$curl http://getcomposer.org/installer | php
  31. 31. Download ComposerDownload composer using command$curl http://getcomposer.org/installer | phpShashi-MacBook-Pro:BehatDemo user$ curl http://getcomposer.org/installer | php % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed100 7606 100 7606 0 0 59636 0 --:--:-- --:--:-- --:--:-- 165k#!/usr/bin/env phpAll settings correct for using ComposerDownloading...Composer successfully installed to: /Users/user/BehatDemo/composer.pharUse it: php composer.phar
  32. 32. Install composer.phar$php composer.phar installYou can also use$php composer.phar update
  33. 33. Install composer.phar$php composer.phar installShashi-MacBook-Pro:BehatDemo user$ php composer.phar updateUpdating dependencies- Installing guzzle/guzzle (v2.6.3) Downloading: 100%- Installing symfony/yaml (dev-master) Cloning 49ad500448c17a4b515c8f567dbb540af3cf41ef- Installing symfony/translation (dev-master) [So on …...........]
  34. 34. Start Behat$ ./bin/behat --init
  35. 35. Start Behat$ ./bin/behat --initShashi-MacBook-Pro:BehatDemo user$ ./bin/behat --init+d features - place your *.feature files here+d features/bootstrap - place bootstrap scripts and static files here+f features/bootstrap/FeatureContext.php - place your feature related code here[You will See Behat created features and bootstrap directory for you]
  36. 36. Behat Did Something ?Created features directoryCreated bootstrap directory and FeatureContext.php file where we will implement step definitions.
  37. 37. Mink ExtensionWe need FeatureContext class toBehat MinkExtensionContextMinkContextSo, We are using MinkExtension which automatically create Mink instance.We used Mink APIs to automated Browser actions. (http://mink.behat.org/api/)
  38. 38. Now, We are ready to use Behat, Mink, Selenium and stuff. Run Behat with $ ./bin/behat
  39. 39. Feature File
  40. 40. How it Looks Like?Feature: featureIn order to [ add feature]As a [role]I want [featute]Scenario: First ScenarioGiven [Context]When [Action]Then [OutCome]
  41. 41. BlogSearch.featureFeature: Blog SearchIn order to search blogsAs a sauce userI need see blogs@javascriptScenario: Search Behat BlogsGiven I am on "/blog"When I fill in "s" with "behat"And I press "Search"Then I should see "Adding Sauce To Behat"[This file should be in /features directory. ]
  42. 42. What will Help to write feature?Behat Cheat Sheethttp://blog.lepine.pro/php/ressources-tutos-Mink Apishttp://mink.behat.org/api/
  43. 43. I know, Too Boring to type ?
  44. 44. I know, Too Boring to type ?Let Me Show You Something Which Will do this in a Minute
  45. 45. Sublime Text2 : Behat- Mink Snippets.
  46. 46. Who Wrote Awesome SnippetsWillemsen ChristopheGitHub : kwattro- Intranet appz developer for the Belgian Defense, BDD practitioner. [ Symfony]Twitter : @ikwattroGitHub: https://github.com/kwattro/sublime- behat-snippets
  47. 47. Dont forgetTo tag Scenarios with “@javascript” tag &Launch our Lovely Selenium Server$ java -jar selenium-server-standalone-2.21.0.jar
  48. 48. Now Run $./bin/behatWatch Test Running in a Browser
  49. 49. ATDD with Behat & SeleniumHow should I find locators before Implementation?
  50. 50. ATDD with Behat & SeleniumHow should I find locators before Implementation?Dont worry, Write high level tests and Behat & Mink APIs will help you to implement Step def later (after implementation)
  51. 51. atdd.featureFeature: ATDDIn order to implement ATDDAs a QAI need to write acceptance tests first @javascript @atdd Scenario: Searching Given I am on blogs page When I search for "chips" Then I should see "No Posts"
  52. 52. Implement Steps/** * @When /^I search for "([^"]*)"$/ */    public function iSearchFor($key) {   $this->fillField("s", $key);        $this->pressButton("Search"); }    /** * @Given /^I am on blogs page$/ */    public function iAmOnBlogsPage()    {        $this->visit("/blog");    }
  53. 53. Run Scenarios with atdd tags$ ./bin/behat --tags="atdd"
  54. 54. Generate ReportTo Generate HTML reports$ ./bin/behat -f html --out report.htmlYou will see report.html generated which looks awesome in browser !!
  55. 55. report.html
  56. 56. Get Code$git clone git@github.com:Shashikant86/BehatDemo.git$cd BehatDemo$curl http://getcomposer.org/installer | php$php composer.phar installDownload Selenium Server and launch server$ java -jar selenium-server-standalone-2.21.0.jar$./bin/behat
  57. 57. Run ANT buildShashi-MacBook-Pro:BehatDemo user$ ant runBuildfile: /Users/user/BehatDemo/build.xmlrun: [delete] Deleting directory /Users/user/BehatDemo/report [mkdir] Created dir: /Users/user/BehatDemo/reportbehat:BUILD SUCCESSFUL
  58. 58. ReportsWe have created report directory to save test report of the latest buildReport will generate in html format in the report/report.html file.
  59. 59. Video DemoHow To clone Repo and usehttp://youtu.be/Apt-1_94zCEThings We didhttp://tinyurl.com/d79s4oy
  60. 60. Linkshttp://behat.org/http://mink.behat.org/https://github.com/Shashikant86/BehatDemohttps://github.com/kwattro/sublime-behat-snippetshttp://everzet.com/
  61. 61. Questions?
  62. 62. Thank You

×