User Level: 

What is Behat?

Behat is a tool that makes Behavior Driven Development (BDD) possible. The following article will step out how to set up Behat for both a Mac and Windows environment.

Before getting started, you'll want to have a few things at your disposal.

  • Administrative access to your computer
  • A local site that you're ready to test
  • A good code editor

Set-Up for Mac

This folder needs to exist outside of the site you want to test. For example, you can create the folder right at the top of your C: directory.

If you're using terminal, go to your User directory and enter the following:

mkdir yourtestfolder
cd yourtestfolder

Composer is an application-level package manager for the PHP programming language that provides a standard format for managing dependencies of PHP software and required libraries.

Composer is used in Behat to manage the different applications that Behat needs to operate.

Composer Set-Up

Install Composer using your terminal.

curl -s | php

Create a composer.json file to tell Composer what to install. To do that, paste the following code into your code editor and save as composer.json. The Drupal Extension requires Behat, Mink, and the Mink Extension. They will all be set up because they’re dependencies of the Drupal Extension, so you don’t have to specify them directly in the composer.json file:

  "require": { 
     "drupal/drupal-extension": "~3.0" 
      "config": { "bin-dir": "bin/" 

For Drupal 8, you'll need to specify the correct version of Guzzle:

    "require": { 
        "drupal/drupal-extension": "~3.0", 
        "guzzlehttp/guzzle" : "^[email protected]" 
    "config": { 
        "bin-dir": "bin/" 

Run the following command to install the Drupal Extension and all of the dependencies. This will probably take awhile before you start to see output.

php composer.phar install

Configure your testing environment by creating a file called behat.yml with the following code. Be sure that you point the base_url at the web site YOU intend to test. Do not include a trailing slash on the base_url.

         - FeatureContext
         - Drupal\DrupalExtension\Context\DrupalContext
         - Drupal\DrupalExtension\Context\MinkContext
         - Drupal\DrupalExtension\Context\MessageContext
         - Drupal\DrupalExtension\Context\DrushContext
       goutte: ~
       selenium2: ~
       base_url: http://seven.l
       blackbox: ~

Type the following in your terminal:

bin/behat --init

When Behat is initialized, the system creates the features folder with some basic things included to get you started, including your own FeatureContext.php file.

use Behat\Behat\Tester\Exception\PendingException; 
use Drupal\DrupalExtension\Context\RawDrupalContext; 
use Behat\Behat\Context\SnippetAcceptingContext; 
use Behat\Gherkin\Node\PyStringNode; 
use Behat\Gherkin\Node\TableNode; 

  * Defines application features from the specific context. 
class FeatureContext extends RawDrupalContext implements SnippetAcceptingContext { 

     * Initializes context. 
     * Every scenario gets its own context instance. 
     * You can also pass arbitrary arguments to the 
     * context constructor through behat.yml. 
     public function __construct() { 

This FeatureContext.php will be aware of both the Drupal Extension and the Mink Extension, so you'll be able to take advantage of their drivers and add your own custom step definitions as well.

To test for appropriate set up, type:

bin/behat -dl

You'll see a list of steps like the following, but longer, if you've installed everything successfully:

default | Given I am an anonymous user 
default | Given I am not logged in 
default | Given I am logged in as a user with the :role role(s) 
default | Given I am logged in as :name

Home Brew is a package manager for Mac OS. Install this into your Mac to then pull in and manage Phantomjs.

Paste this code into your terminal:

/usr/bin/ruby -e "$(curl -fsSL"

PhantomJS is, simply, a web browser without a graphical user interface.

Phantomjs provides a browser environment that Behat can use to run its tests on.

Phantomjs Set-Up

To install phantomjs, type the following into your terminal:

brew install phantomjs
Start Phantomjs

In your console, type

phantomjs --webdriver=8643

Phantomjs will run in the background in this window. To run your Behat tests, open a brand new terminal window.


Set-Up for Windows