Behat Drupal Extension Set up Tests

Profile picture for user devraj

In previous videos, we have installed Drupal extension stand alone and system wide, because of system wide installation we can use composer command directly instead of composer.phar and we can write behat command directly instead of bin/behat. Now, its time to setup your test.

Step 1: Create behat.yml inside your project root directory. Here we are configuring our 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.

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: ~

Step 2: Initialize behat. This creates the features folder with some basic things to get you started, including your own FeatureContext.php file.

$ bin/behat --init

If you get below error here:

  `Drupal\MinkExtension` extension file or class could not be located.  

Check for latest stable release here and update composer.json file with the stable version and use composer update command. init command will generate a FeatureContext.php

This FeatureContext.php will be aware of both the Drupal Extension and the Mink Extension, so you’ll be able to take advantage of their drivers add your own custom step definitions as well. 

Step 3: To ensure everything setup correct, use below command:

bin/behat -dl

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

 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

alright, we are good to write our first behat scenario running drupal website.

Tags