Submitted by Chris McIntosh on October 17, 2015

There is currently a movement towards including Behavioral Driven Development (BDD) in the Agile Process.  This can be best achieved by marrying Behat, an open source behavior-driven development framework, with Selenium2, best known for creating Quality Assurance Tests for various web applications.

In order to utilize the various Behat extensions that you can use in your tests, you can link your Behat Test Suite with Selenium2 and take advantage of the various browser web drivers available (Chrome, Firefox, etc). In this article, we will quickly go over how to accomplish this in a Behat Test Environment. We will also review the things to consider if you have automated your Behat Tests.


There are a few things you have to download before you start going through this tutorial. I have a list of them below along with links to the pages where you can find them.

  • Selenium2 Standalone Server
  • Selenium2 Chrome Webdriver
  • Selenium2 Firefox Webdriver
  • Latest Version of Java
  • Behat

Setting Up

Luckily, configuring the software is a pretty straightforward process. Place your Selenium2 Standalone Server jar file and the web drivers you downloaded in a folder somewhere on your system. Then, from the command line, navigate to the files and run the Standalone Server jar like this:

java -jar selenium-standalone.jar

This will make your Selenium2 server available for your local tests to use during the testing process. For this tutorial, I will be testing a Drupal site with Behat.

Behat Configuration

In order to take advantage of the Selenium2 Server in your test, you will need to update your behat.yml file for your project as well as include some extensions in your Behat test’s featurecontect.php file.

default: paths: features: 'features' extensions: Behat\MinkExtension\Extension: default_session: selenium2 browser_name: 'chrome' selenium2: capabilities: { "browser": "chrome" }   base_url:   Drupal\DrupalExtension\Extension:   blackbox: ~   api_driver: 'drush'   drush: alias: 'ui-local'   region_map:   login_form: “user-login"

In order to tell Behat to use Selenium2, you will need to set your browser name and capabilities to the browser you want to use. For our example, we are using Chrome but there are web drivers for Firefox, Safari, and IE.

In your featuresContext.php file update your code to something like this:

use Drupal\DrupalExtension\Context\DrupalContext; use Behat\Behat\Context\SnippetAcceptingContext; use Behat\Gherkin\Node\PyStringNode; use Behat\Gherkin\Node\TableNode; /** * Defines application features from the specific context. */ class FeatureContext extends Behat\MinkExtension\Context\MinkContext { /** * Initializes context. * * Every scenario gets its own context instance. * You can also pass arbitrary arguments to the * context constructor through behat.yml. */ public function __construct() { } }

Note: The above code sample is set up to use DrupalContext which provides some out-of-the-box functions that can be used when testing a Drupal site.

By following the steps above you can run your Behat Tests on your local machine using Selenium2, a powerful combination for supporting BDD. Later on I will talk about how to integrate this setup with Continuous Integration and Remote Selenium Servers in order to provide better testing for your projects.