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

Behat

Han
October 23, 2012

 Behat

How to use Behat to automate functional testing.

Han

October 23, 2012
Tweet

More Decks by Han

Other Decks in Programming

Transcript

  1. Behat • Behat is a Behavior Driven Development / Acceptance

    Testing Framework based off of Cucumber • Written in PHP • Tests are written in natural language allowing non-technical personnel to participate in the QA process Behat Gherkin Mink Selenium2 Xvfb Firefox
  2. Gherkin • A gherkin is a vegetable similar in form

    and nutritional value to a cucumber... Behat Gherkin Mink Selenium2 Xvfb Firefox
  3. Gherkin • Gherkin is a domain specific language that maps

    English phrases via regex to programming language functions. • It specifies scenarios (test cases): • Given: initial condition • When: user actions • Then: expected final conditions Behat Gherkin Mink Selenium2 Xvfb Firefox
  4. Mink • Mink is a browser abstraction layer • It

    provides a unified API to control a variety of different web browsers: • Goutte - text based, Symfony2 headless browser. No javascript. • Selenium2 - controls most major production browsers Behat Gherkin Mink Selenium2 Xvfb Firefox
  5. Selenium2 • Selenium2 is a browser driver for automating user

    specified events in Chrome, Firefox, and Internet Explorer • Uses Google WebDriver to control a web browser programmatically and supports a variety of languages Behat Gherkin Mink Selenium2 Xvfb Firefox
  6. Xvfb • X virtual framebuffer • Xvfb creates a virtual

    graphical X environment on Linux to run applications without a monitor • Allows us to run Firefox headlessly on a server, enabling automation of tests in a js environment without overhead Behat Gherkin Mink Selenium2 Xvfb Firefox
  7. Firefox • Firefox is... • Oh wait, you guys know

    Firefox Behat Gherkin Mink Selenium2 Xvfb Firefox
  8. Stack Interaction @javascript Behat Mink Goutte Selenium2 Xvfb Firefox Test

    Result Gherkin Custom Gherkin Statements + PHP Execution
  9. Test Organization • To generate a test harness: ./symfony behat:generate-feature

    <app> <module> • Tests are located at: test/features/<app_name>/<module_name>.feature • Each module has its own set of tests, and all the tests for that module are defined in <module_name>.feature
  10. Writing a Test • A Behat test is written in

    Gherkin which is human readable English • Each test is a scenario defined within a feature; there are usually many scenarios for each feature • To get a list of predefined Gherkin statements that you can use: behat -dl
  11. Structure of a Test • A feature corresponds to a

    module • A scenario corresponds to a user interaction with that module and defines a start page, some actions, along with the expected results • A scenario annotated with @javascript will be run using Selenium2 + Firefox
  12. Writing a Test - Example # test/features/frontend/start.feature Feature: Registration and

    Login In order to interact with the gays.com community As a website user I need to be able to register and login to the site Scenario: Login - success Given I am on "/frontend_dev.php" When I fill in "loginEmail" with "[email protected]" And I fill in "loginPass" with "blabla" And I press "Login" Then I should be on "/frontend_dev.php/main"
  13. Running a Test • To run a specific test: behat

    test/features/<app_name>/<module_name>.feature • To run the entire test suite: behat test/features/
  14. Customizing a Test # test/features/frontend/start.feature Feature: Registration and Login In

    order to interact with the gays.com community As a website user I need to be able to register and login to the site Scenario: Login - success Given I am on "/frontend_dev.php" When I fill in "loginEmail" with "[email protected]" And I fill in "loginPass" with "blabla" And I perform a custom task with “parameter” And I press "Login" Then I should be on "/frontend_dev.php/main"
  15. What Happens $ cd <app_root>; behat -v test/features/frontend/start.feature Feature: Registration

    and Login In order to interact with the gays.com community As a website user I need to be able to register and login to the site Scenario: Login - success # start.feature:6 Given I am on "/frontend_dev.php" # FeatureContext::visit() When I fill in "loginEmail" with "[email protected]" # FeatureContext::fillField() And I fill in "loginPass" with "blabla" # FeatureContext::fillField() And I run a custom task with "parameter" And I press "Login" # FeatureContext::pressButton() Then I should be on "/frontend_dev.php/main" # FeatureContext::assertPageAddress() 1 scenario (1 undefined) 6 steps (3 passed, 2 skipped, 1 undefined) 0m6.031s You can implement step definitions for undefined steps with these snippets: /** * @Given /^I run a custom task with "([^"]*)"$/ */ public function iRunACustomTaskWith($argument1) { throw new PendingException(); }
  16. Implementing some Custom Gherkin <?php ... // test/features/frontend/bootstrap/FeatureContext.php class FeatureContext

    extends Behat\Mink\Behat\Context\MinkContext { public function __construct($params) { parent::__construct($params); } /** * @Given /^I run a custom task with "([^"]*)"$/ */ public function iRunACustomTaskWith($argument1) { echo "I'm going to print now: $argument1\n"; } }
  17. Result - Success! $ cd <app_root>; behat -v test/features/frontend/start.feature Feature:

    Registration and Login In order to interact with the gays.com community As a website user I need to be able to register and login to the site Scenario: Login - success # start.feature:6 Given I am on "/frontend_dev.php" # FeatureContext::visit() When I fill in "loginEmail" with "[email protected]" # FeatureContext::fillField() And I fill in "loginPass" with "blabla" # FeatureContext::fillField() I'm going to print now: parameter And I run a custom task with "parameter" And I press "Login" # FeatureContext::pressButton() Then I should be on "/frontend_dev.php/main" # FeatureContext::assertPageAddress() 1 scenario (1 passed) 5 steps (5 passed) 0m5.408s $
  18. Further Resources • http://sylvester:8001/projects/gays/wiki/Behat • http://sylvester:8001/projects/kaufmich/wiki/Behat_-_Functional_Testing • http://behat.org/ • http://mink.behat.org/

    • https://github.com/Behat/Behat • https://github.com/Behat/Mink • http://groups.google.com/group/behat • http://www.symfony-project.org/plugins/sfBehatPlugin