Contributed Module Subcontexts

Although not yet a wide-spread practice, the Drupal Extension to Behat and Mink makes it easy for maintainers to include custom step definitions in their contributed projects.

Discovering SubContexts

In order to use contributed step definitions, define the search path in the behat.yml

// sites/default/behat-tests/behat.yml

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
default:
  paths:
    features: 'features'
  extensions:
    Behat\MinkExtension\Extension:
      goutte: ~
      selenium2: ~
      base_url: http://seven.l
    Drupal\DrupalExtension\Extension:
      blackbox: ~
      api_driver: 'drupal' 
      drush:
        alias: 'local'
      drupal: 
        drupal_root: '/var/www/seven/drupal' 
      region_map:
        footer: "#footer"
      subcontexts:
        paths:
          - "/var/www/seven/drupal/sites/all"

The Drupal Extension will search recursively within the directory or directories specified to discover and load any file ending in behat.inc This system, although created with Drupal contrib projects in mind, searches where it’s pointed, so you can also use it for your own subcontexts, a strategy you might employ to re-use step definitions particular to your shop or company’s development patterns.

Disable autoloading

Autoloading can be disabled in the behat.yml file temorarily with the following:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
default:
  paths:
    features: 'features'
  extensions:
    Behat\MinkExtension\Extension:
      goutte: ~
      selenium2: ~
      base_url: http://seven.l
    Drupal\DrupalExtension\Extension:
      blackbox: ~
      api_driver: 'drupal' 
      drush:
        alias: 'local'
      drupal: 
        drupal_root: '/var/www/seven/drupal' 
      region_map:
        footer: "#footer"
      subcontexts:
        paths:
          - "/var/www/seven/drupal/sites/all"
        autoload: 0

For Contributors

Read a detailed discussion of using sucontexts on the Behat site.

With regard to the Drupal Extension:

  • Save custom step definitions in a file ending with .behat.inc Just like functions, preface the filename with the project’s machine name to prevent namespace collisions.
  • Writing step definitions for the subcontext is only slighly different than writing them for the main context.

Your subcontext must include, at a minimum, lines 7 and 8 below.

// sites/all/modules/beanslide/beanslide.behat.inc

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
<?php
/**
 * @file
 * Provide Behat step-definitions as Drupal Extension subcontexts.
 */

use Drupal\DrupalExtension\Context\DrupalSubContextInterface;
use Behat\Behat\Context\BehatContext;

class BeanslideSubContext extends BehatContext implements 
  DrupalSubContextInterface {
  public static function getAlias() {
    return 'beanslide';
  }
}

You’ll also need to call the main context in order to access the session:

// sites/all/modules/beanslide/beanslide.behat.inc

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
<?php 

  /**
   * @Then /^I should see a slideshow$/
   */
  public function assertSlideshowExists() {
    $slideshow = $this->getMainContext()->getSession()->getPage()
    ->find('css', '.beanslide');
    if (!$slideshow) {
      throw new \Exception(sprintf("No slideshow found on the page %s", 
      $this->getSession()->getCurrentUrl()));
    }
  }