5. Behat Documentation
“It’s the idea that you start by writing human-
readable sentences that describe a feature of
your application and how it should work, and only
then implement this behaviour in software.”
8. Scenario
Given [Context]
And [More context]
!
When (I do) [Action]
And [Other Action]
!
Then (I should see) [Outcome]
And [More Outcomes]
It’s called
Gherkin
btw
9. Given
• Will almost always be the URL of the page to test
!
!
Given /^(?:|I )am on (?:|the )homepage$/
Given /^(?:|I )am on "(?P<page>[^"]+)"$/
• Example:
Given I am on “/credit-card”
10. When
• Any user action - press a button, fill a the textbox,
select a select box
When /^(?:|I )go to "(?P<page>[^"]+)"$/
When /^(?:|I )reload the page$/
When /^(?:|I )move backward one page$/
When /^(?:|I )move forward one page$/
When /^(?:|I )press "(?P<button>(?:[^"]|")*)"$/
When /^(?:|I )follow "(?P<link>(?:[^"]|")*)"$/
When /^(?:|I )fill in "(?P<field>(?:[^"]|")*)" with "(?P<value>(?:[^"]|")*)"$/
When /^(?:|I )fill in "(?P<field>(?:[^"]|")*)" with:$/
When /^(?:|I )fill in "(?P<value>(?:[^"]|")*)" for "(?P<field>(?:[^"]|")*)"$/
When /^(?:|I )fill in the following:$/
When /^(?:|I )select "(?P<option>(?:[^"]|")*)" from "(?P<select>(?:[^"]|")*)"$/
When /^(?:|I )check "(?P<option>(?:[^"]|")*)"$/
When /^(?:|I )uncheck "(?P<option>(?:[^"]|")*)"$/
When /^(?:|I )attach the file "(?P[^"]*)" to “(?P<field>(?:[^"]|")*)"$/
• Example:
When I press “More Info”
11. Then
• What should be seen / happen on the page
Then /^(?:|I )should be on "(?P<page>[^"]+)"$/
Then /^(?:|I )should be on (?:|the )homepage$/
Then /^the response status code should be (?P<code>d+)$/
Then /^the response status code should not be (?P<code>d+)$/
Then /^(?:|I )should see "(?P<text>(?:[^"]|")*)"$/
Then /^(?:|I )should not see "(?P<text>(?:[^"]|")*)"$/
Then /^(?:|I )should see text matching (?P<pattern>"(?:[^"]|")*")$/
Then /^(?:|I )should not see text matching (?P<pattern>"(?:[^"]|")*")$/
Then /^the response should contain "(?P<text>(?:[^"]|")*)"$/
Then /^the response should not contain "(?P<text>(?:[^"]|")*)"$/
Then /^(?:|I )should see "(?P<text>(?:[^"]|")*)" in the "(?P<element>[^"]*)" element$/
Then /^(?:|I )should not see "(?P<text>(?:[^"]|")*)" in the "(?P<element>[^"]*)" element$/
Then /^the "(?P<element>[^"]*)" element should contain "(?P<value>(?:[^"]|")*)"$/
Then /^the "(?P<element>[^"]*)" element should not contain "(?P<value>(?:[^"]|")*)"$/
Then /^(?:|I )should see an? "(?P<element>[^"]*)" element$/
Then /^(?:|I )should not see an? "(?P<element>[^"]*)" element$/
Then /^the "(?P<field>(?:[^"]|")*)" field should contain "(?P<value>(?:[^"]|")*)"$/
Then /^the "(?P<field>(?:[^"]|")*)" field should not contain "(?P<value>(?:[^"]|")*)"$/
Then /^(?:|I )should see (?P<num>d+) "(?P<element>[^"]*)" elements?$/
Then /^print current URL$/
Then /^print last response$/
• Example:
Then I should be on “Maybank Gold Card 2”
13. Scenario
Given I am on “/credit-card”
!
When I hover on “.rating-icon”
!
Then I should see “3% cashback”
14. Scenario
Given I am on “/credit-card”
!
When I check “#petrol-filter”
And I press “Filter”
!
Then I should see [Petrol] card
And I should not see [Groceries] card
And I should not see [Travel] card
And I should not see [Shopping] card
And I should not see [Bills] card
16. Recap
• Think about the behaviours you want
• Write scenarios that describe those behaviours
• Start a failing test
• Write code for it to pass
• Restart test = green