You are here

BDD with Behat and Drupal

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:

{
    "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!

SHARE SOCIAL
Developer
Nazli moved to Montreal, Canada to study Computer Science at Concordia University in 2009. During her school years, she was part of many team projects, joined many Hack-a-thons and meet ups. After obtaining her degree at Concordia, Nazli started learning new technologies on her own, including...→ More about Nazli
Developer

Nazli moved to Montreal, Canada to study Computer Science at Concordia University in 2009. During her school years, she was part of many team projects, joined many Hack-a-thons and meet ups. After obtaining her degree at Concordia, Nazli started learning new technologies on her own, including Drupal and PhoneGap.

Joining Appnovation helped Nazli see her potential as a developer. As a part of the Appnovation team, she knew there are many outlets to obtain her persoal career goals, the definite possiblity of being a part of challenging projects, and to be able to show her passion in programming by working with very talented and innovative people. 

Last place I visited: Istanbul and Madrid

Music I am listening to: Feist, Placebo, Sigur Ros

Favourite Books: The Little Prince(by Exupery) , An Eternal Golden Braid(by Hofstadter)

Appnovation Technologies