Drush Driver

Many tests require that a user logs into the site. With the blackbox driver, all user creation and login would have to take place via the user interface, which quickly becomes tedious and time consuming. You can use the Drush driver to add users, reset passwords, and log in by following the steps below, again, without having to write custom PHP. You can also do this with the Drupal API driver. The main advantage of the Drush driver is that it can work when your tests run on a different server than the site being tested.

Install Drush

See the Drush project page for installation directions.

Point Drush at your Drupal site

Drupal Alias (For local or remote sites)

You’ll need ssh-key access to a remote server to use Drush. If Drush and Drush aliases are new to you, see the Drush site for detailed examples

The alias for our example looks like:

1
2
3
4
5
6
7
8
9
<?php
$aliases['local'] = array(
  'root' => '/var/www/seven/drupal',
  'uri'  =>  'seven.l'
);
$aliases['git7site'] = array(
  'uri'  =>  'git7site.devdrupal.org',
  'host' => 'git7site.devdrupal.org'
);

Path to Drupal (local sites only)

If you’ll only be running drush commands to access a site on the same machine, you can specify the path to your Drupal root:

1
2
3
4
 Drupal\DrupalExtension:
   blackbox: ~
 drush:
   root: /my/path/to/drupal

Enable the Drush driver

In the behat.yml file:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
default:
  suites:
    default:
      contexts:
        - FeatureContext
        - Drupal\DrupalExtension\Context\DrupalContext
        - Drupal\DrupalExtension\Context\MinkContext
  extensions:
    Behat\MinkExtension:
      goutte: ~
      selenium2: ~
      base_url: http://seven.l
    Drupal\DrupalExtension:
      blackbox: ~
      api_driver: 'drush' 
      drush:
        alias: 'local'
      region_map:
        footer: "#footer"

Note

Line 15 isn’t strictly necessary for the Drush driver, which is the default for the API.

Calling the Drush driver

Untagged tests use the blackbox driver. To invoke the Drush driver, tag the scenario with @api

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
Feature: Drush alias
  In order to demonstrate the Drush driver
  As a trainer
  I need to show how to tag scenarios 

  Scenario: Untagged scenario uses blackbox driver and fails
    Given I am logged in as a user with the "authenticated user" role
    When I click "My account"
    Then I should see the heading "History"

  @api
  Scenario: Tagged scenario uses Drush driver and succeeds
    Given I am logged in as a user with the "authenticated user" role
    When I click "My account"
    Then I should see the heading "History"

If you try to run a test without that tag, it will fail.

Example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
Feature: Drush alias
  In order to demonstrate the Drush driver
  As a trainer
  I need to show how to tag scenarios

  Scenario: Untagged scenario uses blackbox driver and fails
    # features/drush.feature:6
    Given I am logged in as a user with the "authenticated user" role 
    # FeatureContext::iAmLoggedInWithRole()
      No ability to create users in Drupal\Driver\BlackboxDriver. 
      Put `@api` into your feature and add an api driver 
      (ex: `api_driver: drupal`) in behat.yml.
    When I click "My account"                                         
    # FeatureContext::iClick()
    Then I should see the heading "History"                           
    # FeatureContext::assertHeading()

  @api
  Scenario: Tagged scenario uses Drush driver and succeeds            
            # features/drush.feature:12
    Given I am logged in as a user with the "authenticated user" role 
    # FeatureContext::iAmLoggedInWithRole()
    When I click "My account"                                         
    # FeatureContext::iClick()

The Drush driver gives you access to all the blackbox steps, plus those used in each of the following examples:

 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
@api
Feature: Drush driver
  In order to show functionality added by the Drush driver 
  As a trainer
  I need to use the step definitions it supports

  Scenario: Drush alias
    Given I am logged in as a user with the "authenticated user" role
    When I click "My account"
    Then I should see the heading "History"

  Scenario: Target links within table rows
    Given I am logged in as a user with the "administrator" role
    When I am at "admin/structure/types"
    And I click "manage fields" in the "Article" row
    Then I should be on "admin/structure/types/manage/article/fields"
    And I should see text matching "Add new field"

  Scenario: Clear cache
    Given the cache has been cleared
    When I am on the homepage
    Then I should get a "200" HTTP response

  Scenario: Create and log in as a user
    Given I am logged in as a user with the "authenticated user" role
    When I click "My account"
    Then I should see the heading "History"

  Scenario: Target links within table rows
    Given I am logged in as a user with the "administrator" role
    When I am at "admin/structure/types"
    And I click "manage fields" in the "Article" row
    Then I should be on "admin/structure/types/manage/article/fields"
    And I should see text matching "Add new field"