• How to set up Behat to run tests for Drupal 8 projects.
  • How to write non-javascript enabled tests using driver Goutte driver.
  • How to write javascript enabled tests using the Selenium driver.
  • How to configure Selenium to run tests from a virtual machine.


General behat configuration goes in behat.yml in repo root:

contexts are the PHP code translating gherkin into actionable tasks

Gherkin stuff goes in features directory in repo root


Contexts defined in behat yml translate Gherkin to actions in PHP:

  • Arguments derived from items in "quotes" in Gherkin:
     * @When I fill in the :arg1 field with :arg2
    public function iFillInTheFieldWith($name, $value)
      $selector = $this->getFieldSelector($name);
      $field = $this->fixStepArgument($selector);
      $value = $this->fixStepArgument($value);
      $this->getSession()->getPage()->fillField($field, $value);

Gherkin can utilize the drupal behat extension that has prebuilt contexts.

This is accomplished by using
in Gherkin (see above)

Remember: ./vendor/bin/behat may be ran w/o args to perform all tests

Can also run specific tests by specifying them alongside behat cmd

Issues w/ things like javascript and behat.yml driver definition

"Driver" (as defined in behat.yml) is important
Example: Javascript won't be available to the behat default browser, which can cause some tests to fail.

In Gherkin: tagging w/ @javascript (alongside, for example, @api) can indicate that a JS-enabled browser should be used for running the associated test.

Note: Chrome CLI (ie headless chrome) needed to allow mink to control non-terminal, JS-enabled browser appropriately.

behat.yml mink config for chrome CLI usage in example code:

      goutte: ~
      default_session: goutte
      javascript_session: default
      browser_name: chrome
      base_url: "http://localhost:8888"
            api_url: "http://localhost:9222"