The "Drupal Extension to Behat and Mink" provides some nice Drupal specific step definitions and ways of setting test data up. This post runs through the setup process.

First create a behat directory outside your Drupal installation (public_html in my case) so that you have the following directory layout:

.
├── behat
├── logs
│   ├── access.log
│   └── error.log
├── public_html
│   ├── autoload.php
│   ├── ...
│   └── web.config
├── sessions
├── tmp
└── upload

I always keep the entire layout show above in git (with git ignores on the contents of logs , session , tmp and upload ).

Inside the behat directory create a composer.json file, with the following content:

{
  "require": {
    "drupal/drupal-extension": "~3.0",
    "guzzlehttp/guzzle": "^6.0@dev",
    "symfony/dependency-injection": "2.8.2",
    "symfony/event-dispatcher": "2.8.2"
  },
  "config": {
    "bin-dir": "bin/"
  }
}

It is needed to pin the version of the Symfony dependencies, else you will get this error:

PHP Fatal error:  Undefined class constant 'Symfony\Component\DependencyInjection\ContainerInterface::SCOPE_CONTAINER' in /var/www/drupal8.dev/public_html/core/lib/Drupal/Core/DependencyInjection/Container.php on line 16

See this bug report for more info. The file is based on the Stand-alone installation documentation for the Drupal Extension to Behat and Mink.

Next the dependencies has to be installed. I use my Docker based dev environment "pilotboat", so I execute pilotboat shell to get a shell inside the container, if you use something else, just follow the next instructions.

Enter the behat directory, cd drupal8.dev/behat and run: composer install and wait until everything is installed.

Now we can create behat.yml . Adapt the url for PhantomJS (wd_host), and the paths to the Drupal installation so it matches your setup.

default:
  suites:
    default:
      contexts:
        - FeatureContext
        - Drupal\DrupalExtension\Context\DrupalContext
        - Drupal\DrupalExtension\Context\MinkContext
        - Drupal\DrupalExtension\Context\MessageContext
        - Drupal\DrupalExtension\Context\DrushContext
  extensions:
    Behat\MinkExtension:
      selenium2:
        wd_host: 'http://phantomjs:8910/wd/hub'
      base_url: http://drupal8.dev
    Drupal\DrupalExtension:
      blackbox: ~
      api_driver: 'drupal'
      drush:
        root: '/var/www/drupal8.dev/public_html'
      drupal:
        drupal_root: '/var/www/drupal8.dev/public_html'

Now everything should be ready so that you can run bin/behat --init and then create the first feature: features/homepage.feature .

Feature: Homepage
  In order to see if the home page works
  As a website user
  I need to be able to see the home page

  Scenario: See the home page
    Given I am on "/"
    Then I should see "Welcome to drupal8.dev"

If you want to create nodes, you can tag the feature or the scenario with "@api":

@api
  Scenario: Create a node
    Given I am logged in as a user with the "administrator" role
    When I am viewing an "article" content with the title "My article"
    Then I should see the heading "My article"

For more information about the api driver see the Drupal API Driver documentation.