Entity view (Content)

BDD with Behat and Drupal

By nbozoglu
Mar. 16, 2015

As a back-end developer, I had the chance to work on a Drupal web development project which required writing automated tests for a Drupal site using Behat. The whole objective of the project was to have tests running on a site sprint after sprint, without the need to change the scripts.

My role in the project included not only writing tests, but also writing PHP functions, which will accommodate the step definitions. Before it starts getting more puzzling, let me give you an example:

Feature: Search button on Google page.
  In order to see the Google Search button
  As a visitor
  I need to be able to visit google.ca

  @javascript
  Scenario: Visit google.ca
    Given I am at "http://www.google.ca"
    Then I should see "Google Search"

This is a very basic example of Behaviour Driven Development with Behat, which uses Gherkin Language. Basically in your test case, all you need to make sure you have is Feature, a tag (i.e. @javascript) and a Scenario. Gherkin uses indentation to define structure so make sure you use tabs between Feature and Scenario, and after Scenario at the each step.

It was my first time using Behat, and even after reading the documentation, I was still not sure how it was working. How does it know what is to "be at somewhere" or to "see something" on the page?

I put on my developer hat and after digging through some of the files, it clicked! There was php functions to accommodate every single line of step definition that we use. There is one example of which we customly created:

/**
 *
 * @When /^I check the "([^"]*)" radio button$/
 *
 */
public function iCheckTheRadioButton($radioButton) {
  $radioButton = $this->getSession()->getPage()->findField($radioButton);
  if (NULL === $radioButton) {
    throw new Exception("Cannot find radio button " . $radioButton);
  }
  $value = $radioButton->getAttribute('value');
  $this->getSession()->getDriver()->click($radioButton->getXPath());
}

As you may have already guessed, the comment area is where we match the step definition with its function. This way, when we include, for instance, "When I check the "module" radio button" step, we basically are calling that function. The key point here is to be sure to use the exact same words in the step definition as the one we include in the comment section of our function.

If you would like to dive into writing test cases for Behaviour Driven Development, here is what you will need:

  • php > 5.3.5
  • java
  • Firefox
  • Selenium Server (available here http://docs.seleniumhq.org/download/)
  • composer.json with the following minimum package requirement:
{
    "require": {
        "behat/behat": "2.4.*@stable"
    },
    "minimum-stability": "dev",
    "config": {
        "bin-dir": "bin/"
    }
}

and then all you need to do is run:

$ curl http://getcomposer.org/installer | php
$ php composer.phar install


After this, you need to test if everything is done correctly by running:

$ bin/behat

 

BONUS: There is a Drupal extension (not a module) for Behat and Mink which comes with more step definitions such as testing for a specific region and testing as a certain user (i.e. an anonymous user). In order to have the packege, all you need to do is to require the drupal extension in the composer file, such as :

{
  "require": {
    "drupal/drupal-extension": "~3.0"
}

in addition to the existing composer.json file. And don't forget to run 'composer update' after if you make this addition on top of what you already have.

Till next time!

Post Tags: