System-wide installation

A system-wide installation allows you to maintain a single copy of the testing tool set and use it for multiple test environments. Configuration is slightly more complex than the stand-alone installation but many people prefer the flexibility and ease-of-maintenance this setup provides.

Overview

To install the Drupal Extension globally:

  1. Install Composer
  2. Install the Drupal Extension in /opt/drupalextension
  3. Create an alias to the behat binary in /usr/local/bin
  4. Create your test folder

Install Composer

Composer is a PHP dependency manager that will make sure all the pieces you need get installed. Full directions for global installation and more information can be found on the Composer website.:

curl -sS https://getcomposer.org/installer |
php mv composer.phar /usr/local/bin/composer

Install the Drupal Extension

  1. Make a directory in /opt (or wherever you choose) for the Drupal Extension:

    cd /opt/
    sudo mkdir drupalextension
    cd drupalextension/
    
  1. Create a file called composer.json and include the following:
1
2
3
4
5
6
7
8
{
  "require": {
    "drupal/drupal-extension": "^3.2"
  },
  "config": {
    "bin-dir": "bin/"
  }
}
  1. Run the install command:

    composer install
    
It will be a bit before you start seeing any output. It will also suggest that you install additional tools, but they’re not normally needed so you can safely ignore that message.
  1. Test that your install worked by typing the following:

    bin/behat --help
    
If you were successful, you’ll see the help output.
  1. Make the binary available system-wide:

    ln -s /opt/drupalextension/bin/behat /usr/local/bin/behat
    

Set up tests

  1. Create the directory that will hold your tests. There is no technical reason this needs to be inside the Drupal directory at all. It is best to keep them in the same version control repository so that the tests match the version of the site they are written for.

One clear pattern is to keep them in the sites folder as follows:

Single site: sites/default/behat-tests

Multi-site or named single site: /sites/my.domain.com/behat-tests

  1. Wherever you make your test folder, inside it create the behat.yml file:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
default:
  suites:
    default:
      contexts:
        - FeatureContext
        - Drupal\DrupalExtension\Context\DrupalContext
        - Drupal\DrupalExtension\Context\MinkContext
        - Drupal\DrupalExtension\Context\MessageContext
        - Drupal\DrupalExtension\Context\DrushContext
  extensions:
    Drupal\MinkExtension:
      goutte: ~
      selenium2: ~
      base_url: http://seven.l
    Drupal\DrupalExtension:
      blackbox: ~
  1. Initialize behat. This creates the features folder with some basic things to get you started:

    bin/behat --init
    
  2. This will generate a FeatureContext.php file that looks like:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?php

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 will make your FeatureContext.php aware of both the Drupal Extension and the Mink Extension, so you’ll be able to take advantage of their drivers and step definitions and add your own custom step definitions here. The FeatureContext.php file must be in the same directory as your behat.yml file otherwise in step 5 you will get the following error:

[BehatBehatContextExceptionContextNotFoundException] FeatureContext context class not found and can not be used.
  1. To ensure everything is set up appropriately, type:

    behat -dl
    

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

1
2
3
4
 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