System-wide installation

A system-wide installation allows you to maintain a single copy of the testing toolset and use it for multiple test environments. Configuuration 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": "1.0.*@stable"
},
  "config": {
    "bin-dir": "bin/"
  }
}
  1. Run the install command:

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

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

Multisite 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
default:
  paths:
    features: 'features'
  extensions:
    Behat\MinkExtension\Extension:
      goutte: ~
      selenium2: ~
      base_url: http://seven.l
    Drupal\DrupalExtension\Extension:
      blackbox: ~
  1. Edit features/bootstrap/FeatureContext.php so that it matches the following:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<?php

use Behat\Behat\Context\ClosuredContextInterface,
    Behat\Behat\Context\TranslatedContextInterface,
    Behat\Behat\Context\BehatContext,
    Behat\Behat\Exception\PendingException;
use Behat\Gherkin\Node\PyStringNode,
    Behat\Gherkin\Node\TableNode;

//
// Require 3rd-party libraries here:
//
//   require_once 'PHPUnit/Autoload.php';
//   require_once 'PHPUnit/Framework/Assert/Functions.php';
//

/**
 * Features context.
 */
// class FeatureContext extends BehatContext 
class FeatureContext extends Drupal\DrupalExtension\Context\DrupalContext
{
    /**
     * Initializes context.
     * Every scenario gets its own context object.
     *
     * @param array $parameters context parameters (set them in behat.yml)
     */
    public function __construct(array $parameters)
    {
        // Initialize your context here
    }

//
// Place your definition and hook methods here:
//
//    /**
//     * @Given /^I have done something with "([^"]*)"$/
//     */
//    public function iHaveDoneSomethingWith($argument)
//    {
//        doSomethingWith($argument);
//    }
//
}

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.

  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
 5
 6
 7
 8
 9
10
11
12
13
 Given /^(?:that I|I) am at "(?P[^"]*)"$/
     - Visit a given path, and additionally check for HTTP response code
       200.
     # Drupal\DrupalExtension\Context\DrupalContext::iAmAt()

  When /^I visit "(?P[^"]*)"$/
     # Drupal\DrupalExtension\Context\DrupalContext::iVisit()

  When /^I click "(?P<link>[^"]*)"$/
     # Drupal\DrupalExtension\Context\DrupalContext::iClick()

 Given /^for "(?P<field>[^"]*)" I enter "(?P<value>[^"]*)"$/
     # Drupal\DrupalExtension\Context\DrupalContext::forIenter()