Stand-alone installationΒΆ

A stand-alone installation is recommended when you want your tests and testing environment to be portable, from local development to CI server, to client infrastructure. It also make documentation consistent and reliable.

  1. Create a folder for your BDD tests:

    mkdir projectfolder cd projectfolder
    
All the commands that follow are written to install from the root of your project folder.
  1. Install Composer, a php package manager:

    curl -s https://getcomposer.org/installer | php
    
  2. Create a composer.json file to tell Composer what to install. To do that, paste the following code into your editor and save as composer.json. The Drupal Extension requires Behat, Mink, and the Mink Extension. The 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:

1
2
3
4
5
6
7
8
{
  "require": {
    "drupal/drupal-extension": "1.0.*@stable"
},
  "config": {
    "bin-dir": "bin/"
  }
}
  1. Run the following command to install the Drupal Extension and all those dependencies. This takes a while before you start to see output:

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

 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. Initialze behat. This creates the features folder with some basic things to get you started, including your own FeatureContext.php file:

    bin/behat --init
    
  2. 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 as well.

  1. To ensure everything is set up appropriately, type:

    bin/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()