This document summarizes a presentation on using Behat and Mink for automated acceptance testing. It discusses how Behat and Mink allow developing automated tests from user stories and acceptance criteria. This ensures developers and clients have a shared understanding of requirements and that new features do not break existing functionality. Tests can be run quickly against different environments and browsers. The presentation provides an overview of Behat and Mink components and how to set up a test suite with Composer. It also covers best practices like writing examples and using tags, profiles and hooks.
1 of 66
More Related Content
Phpday - Automated acceptance testing with Behat and Mink
2. LET ME INTRODUCE MYSELF
@Richard_Tuin
Software developer from the Netherlands
Particular interest in QA
Working for
Saturday, May 18, 13
3. INEFFICIENCY
Different view on scope and resulting product
Clients dont get a feel of how they will use the product
I like what youve built, but it doesnt do what i meant
Thanks for this new feature, but now x stopped working!
Saturday, May 18, 13
4. INEFFICIENCY
Different view on scope and resulting product
Clients dont get a feel of how they will use the product
I like what youve built, but it doesnt do what i meant
Thanks for this new feature, but now x stopped working!
Saturday, May 18, 13
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 quality
Saturday, May 18, 13
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 still
according to acceptance criteria
Saturday, May 18, 13
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 still
according to acceptance criteria
Automation?
Saturday, May 18, 13
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 one
Saturday, May 18, 13
18. FEATURE FILE LAYOUT
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: ...
Information: http://dannorth.net/whats-in-a-story/
Saturday, May 18, 13
19. EXAMPLE FEATURE
Feature: Searching on the internet
As a google.com visitor
I want to use the search engine
So that i can find information on the internet
Scenario: Simple keyword search
Given I am on the homepage
When I fill in the search term PHP
And I press Google search
Then I should see search results containing PHP
Scenario: I feel lucky
Given I am on the homepage
When I fill in the search term PHP
And I press Im feeling lucky
Then I should not be on google.com
And I should see PHP
Saturday, May 18, 13
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. 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, module
Saturday, May 18, 13
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, module
Scenario
Saturday, May 18, 13
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, module
Scenario
Steps
Saturday, May 18, 13
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 involved
Saturday, May 18, 13
32. COMPONENTS OF A BEHAT+MINK
FUNCTIONALTEST SUITE
Features
Feature parser
Generic browser controller client
Browser controller/simulator
Executable code that controls the browser controller client
Saturday, May 18, 13
33. COMPONENTS OF A BEHAT+MINK
FUNCTIONALTEST SUITE
Features
Feature parser
Generic browser controller client
Browser controller/simulator
Executable code that controls the browser controller client
Living documentation
Saturday, May 18, 13
34. COMPONENTS OF A BEHAT+MINK
FUNCTIONALTEST SUITE
Features
Feature parser
Generic browser controller client
Browser controller/simulator
Executable code that controls the browser controller client
Living documentation
Behat
Saturday, May 18, 13
35. COMPONENTS OF A BEHAT+MINK
FUNCTIONALTEST SUITE
Features
Feature parser
Generic browser controller client
Browser controller/simulator
Executable code that controls the browser controller client
Living documentation
Behat
Mink
Saturday, May 18, 13
36. COMPONENTS OF A BEHAT+MINK
FUNCTIONALTEST SUITE
Features
Feature parser
Generic browser controller client
Browser controller/simulator
Executable code that controls the browser controller client
Living documentation
Behat
Mink
Selenium, Zombie.js, Goutte,
etc.
Saturday, May 18, 13
37. COMPONENTS OF A BEHAT+MINK
FUNCTIONALTEST SUITE
Features
Feature parser
Generic browser controller client
Browser controller/simulator
Executable code that controls the browser controller client
Living documentation
Behat
Mink
Selenium, Zombie.js, Goutte,
etc.
MinkExtension
Saturday, May 18, 13
40. HELLO BEHAT
1. Initialize a Behat testsuite skeleton
$ bin/behat --init
2. Behat creates its standard project structure
Saturday, May 18, 13
41. MINKEXTENSION
1. Bridges the gap between Behat and Mink
2. Contains a set of prede鍖ned step de鍖nitions
3. Not all step de鍖nitions may be usable for all projects
4. Behat + Mink is easy:
use BehatMinkExtensionContextMinkContext;
class FeatureContext extends MinkContext
{
// ...
}
features/FeatureContext.php
Saturday, May 18, 13
42. MINKEXTENSION STEPS
Given /^(?:|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. BEHAT.YML
Place for con鍖guration options
Basic Behat / Mink con鍖g
default:
extensions:
BehatMinkExtensionExtension:
base_url: "http://www.yourwebsite.com"
goutte: ~
selenium2: ~
Saturday, May 18, 13
45. EXAMPLE FEATURE
Feature: Search on the internet
As a google.com visitor
I want to use the search engine
So that i can find information on the internet
Scenario: Simple keyword search
Given I am on the homepage
When I fill in the search term PHP
And I press Google search
Then I should see search results containing PHP
features/search.feature
Saturday, May 18, 13
46. STEP DEFINITIONS
/**
* @When /^I fill in the search term "([^"]*)"$/
*/
public function iSearchTheTerm($arg1)
{
throw new PendingException();
}
Put these in your FeatureContext.php 鍖le.
$ bin/behat
Running Behat
Behat gives you step suggestions:
Saturday, May 18, 13
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. MINKTERMINOLOGY
Driver = Browser controller/emulator
Session = Browser
Page = Document(Element)
Element
Selectors
XPath
CSS
Named
Saturday, May 18, 13
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. ELEMENT ACTIONS
$page = $this->getSession()->getPage();
$loginLink = $page->find('css', '#login');
$loginLink->click();
->getValue();
->setValue();
->keyUp();
->press();
etcetera...
Saturday, May 18, 13
53. PROFILES
Full con鍖guration per environment
Feature / Scenario selection based on 鍖lters
De鍖ne them in behat.yml:
Call the pro鍖le with: $ bin/behat --profile acceptance
default:
extension:
BehatMinkExtensionContextMinkContext
base_url: http://www.example.org
acceptance:
extension:
BehatMinkExtensionContextMinkContext
base_url: http://acc.example.org
Saturday, May 18, 13
54. TAGS
Metadata for features and scenarios
Via command line: $ bin/behat --tags @smoke
De鍖ne as 鍖lter in pro鍖le
@smoke
Feature: Search on the www
@javascript
Scenario: ...
search.feature
smoke:
filters:
tags: @smoke
behat.yml
nojs:
filters:
tags: ~@javascript
behat.yml
Saturday, May 18, 13
55. FILTERS
Grouping tests
Tags
Con鍖guration in behat.yml
smoketests:
filters:
tags: @smoketest&&~@wip
development:
filters:
tags: ~@slow&&~@wip
Saturday, May 18, 13
57. SCENARIO OUTLINES
Scenario Outline: Simple keyword search
Given I am on the homepage
When 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
62. LOCALIZATION
Write features in your own language
# language: it
Funzionalit: non so scrivere italiano
Scenario: ...
Dato ...
Quando ...
Allora ...
Schema dello scenario: ...
Dato ...
Quando ...
Allora ...
Esempi:
...
Saturday, May 18, 13
63. INTEGRATION WITH JENKINS
Behat includes export to jUnit format
Jenkins build step example
${WORKSPACE}/bin/behat --format junit --out ${WORKSPACE}/build
Saturday, May 18, 13
64. CONCLUSION
1. Writing examples is great to improve communication
2. Automating acceptance criteria = consistent quality
3. Behat and Mink are an excellent combination to do this
Saturday, May 18, 13