Behat Cheat Sheet

A reference and style guide for Behat and Mink, based on Lepine's and Kudryashov's references and guides.


April 14, 2014

New to Behat?

  1. What is Behavior Driven Development?
  2. BDD with Behat and Mink


  • pear channel-discover
  • pear channel-discover
  • pear install behat/behat

Command Line

Command (w/ behat) Purpose
—init Create the features directory
—config={file}.yml Use config file
—format=html –out=report.html Html Report
—expand Display details
—story-syntax —lang=fr in French
—tags=’@{tag1},@{tag2}’ Run tags


Feature: Descriptive text of what is desired

  In order to realize a named business value
  As an explicit system actor
  I want to ...

    Scenario: Some determinable business situation

      Given some precondition
      And some other precondition
      When some action by the actor
      And some other action
      And yet another action
      Then some testable outcome is achieved
      And something else we can check happens too
      Scenario: A different situation

Using Examples

Scenario Outline: Some determinable business situation

  Given I have <initialAmount> euros
  When I add <money> euros
  Then I should have now <finalAmount> euros

  | initialAmount | money | finalAmount |
  | 15 | 5 | 20 |
  | 40 | 10 | 50 |
  | 20 | 5 | 25 |

Surf Actions

I am on "url"
I go to "url"
I reload the page
I move backward one page
I move forward one page
I press "button"
I follow "link"


I fill in "form_element" with "value"
I fill in "value" for "form_element"
I fill in the following
I select "form_option" from "form_select"
I additionally select "form_option" from "form_select"
I check "form_checkbox"
I uncheck "form_checkbox"
I attach the file "/path/file.file" to "form_file"


I should see "content"
the response should contain "content"
I should not see "content"
the response should not contain "content"
the "form_element" field should contain "value"
the "form_element" field should not contain "value"
the "form_checkbox" checkbox should be checked
the "form_checkbox" checkbox should not be checked
I should be on "page"
the url should match "url"
the "num_position" element should contain "value"
I should see "value" in the "element" element
I should see an "element" element
I should not see an "element" element
I should see number "element" elements
the response status code should be code


  • pear channel-discover
  • pear channel-discover
  • pear install behat/mink-beta


$session = new \Behat\Mink\Session($driver);
$session->start(); // start
$session->reset(); // soft-reset:
$session->restart(); // hard-reset:

From the main context

$session = $this->getSession();

From a sub-context

$session = $this->getMainContext()->getSession();
Step Definition Purpose
isStarted() Checks whether session was started
start() Starts session
stop() Stop session
restart() Restart session
reset() Reset session
getPage() Returns page element
getSelectorHandler() Returns Selector Handler
visit($url) Visit specified URL
setBasicAuth($u,$p) HTTP Basic authentification
setRequestHeader($n,$v) Set request header
getResponseHeaders() Get all response headers
setCookie($n,$v) Sets cookie
getCookie($n) Returns cookie
getStatusCode() Returns response code
getCurrentUrl() Returns current URL
reload() Reload current page
back() Moves backward
forward() Move forward
executeScript($script) Executes javascript
evaluateScript($script) Returns javascript’ response
wait($time, $condition) Waits some time or until javascript condition is true


$el->has($selector, $locator)
$el->find($selector, $locator)
$el->findAll($selector, $locator)

HTML Nodes



$el->selectOption($option, $multiple)



Default behat.yml Configurations

      default_session: goutte
      javascript_session: sahi
      base_url: http://localhost
      browser: firefox
          adapter: Zend\Http\Client\Adapter\Proxy
          proxy_port: 8080
          host: localhost
          port: 9999
          port: 8124
          node_bin: node
          auto_server: true
          host: localhost
          port: 4444
          host http://localhost:4444/wd/hub

Available Drivers

  1. Goutte
  2. Sahi
  3. Zombie
  4. Selenium (1 & 2 )