https://events.drupal.org/seattle2019/sessions/behavior-driven-development-drupal-8-behat

Slides:

https://bit.ly/drupal-behat

  • 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.

Source

https://gist.github.com/dinarcon/29d01dd38351403e671abf257c9e5c99

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

Example:

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
@api
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:

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