Prepare Behat for testing a Drupal site

Behat is an open source Behavior-Driven Development framework for PHP. It can help you test how your site responds to the common or custom user behaviors that you define. To define those test it uses the Gherkin language syntax that makes the test human friendly to read and understand.
Some say that, because of Gherkin, you have to do the same work twice, but once you have defined all the Gherkin steps, things are easier and more maintainable for every one that wants to check, give some feedback, learn or work with.
I’ll give some help to set up your Behat, Mink and PhantomJS to test your site behavior. In my case I did it to test a Drupal site.

First you have to create a directory for your tests (named eg: test) inside your Drupal project to have files organized and you are going to install everything and work everything from this guide from that directory.

Installation

To install Behat and its extensions we will need composer:

$ curl -s https://getcomposer.org/installer | php

Once installed composer, install Behat and all the tools we are going to need. To do so edit your composer.json file to have the “require” like this:

"require": {
    "behat/behat": "^3.1",
    "behat/mink": "^1.7",
    "behat/mink-extension": "^2.2",
    "behat/mink-selenium2-driver": "^1.3",
    "behat/mink-goutte-driver": "^1.2",
    "beberlei/assert": "^2.5",
},


Save, and then execute:

$ composer.phar install

What we are going to install here are:

  • Behat will do things that a regular user does in a web application right? In order to do so, we are going to need Mink (behat/mink). Mink is an open source browser controller/emulator for web applications.
  • Mink Extension is an integration layer between Behat and Mink and it provides:
    • Additional services for Behat (Mink, Sessions, Drivers).
    • Behat\MinkExtension\Context\MinkAwareContext which provides Mink instance for your contexts.
    • Base Behat\MinkExtension\Context\MinkContext context which provides base step definitions and hooks for your contexts or subcontexts. Or it could be even used as context on its own.
  • selenium2-driver (behat/mink-selenium2-driver), it is for automating web applications for testing purposes
  • Goutte Driver (mink-goutte-driver), provides a bridge for the Goutte headless browser. Goutte is a classical pure-php headless browser, written by the creator of the Symfony framework Fabien Potencier.
  • Assert (beberlei/assert) simple php library which contains assertions and guard methods for input validation (not filtering!) in business-model, libraries and application low-level code. The library can be used to implement pre-/post-conditions on input data.
  • Drupal Extension, is an integration layer between Behat, Mink Extension, and Drupal. It provides step definitions for common testing scenarios specific to Drupal sites.

Then we check the installed version of Behat:

$ php behat.phar -V

Edit the behat.yml file at the root of your test directory to look like this:

default:
  autoload:
    '': Features/Bootstrap
  suites:
    test:
      paths:
        - %paths.base%/features
      contexts:
        - Features\Bootstrap\FeatureContext
        - Drupal\DrupalExtension\Context\DrupalContext
        - Drupal\DrupalExtension\Context\MinkContext
        - Drupal\DrupalExtension\Context\MessageContext
        - Drupal\DrupalExtension\Context\DrushContext
  extensions:
    Behat\MinkExtension:
      files_path: 'path_to_test_directory'
      base_url:  'https://www.example.com'
      sessions:
        default:
          goutte:
            guzzle_parameters:
              defaults:
                verify: false
        javascript:
          selenium2:
            browser: chrome

Behat comes with a default suite to test, suites are a way for Behat to know where to find and how to test your application against your features placed in features/ directory. To initialize the default suite execute the command:

$ vendor/bin/behat --init

To see more options you can use:

$ vendor/bin/behat -h

Behat has some available steps definitions by default, to list them use:

$ vendor/bin/behat -dl

Or this for a more detailed list

$ vendor/bin/behat -di

You could define some custom steps, If you want to learn and start defining some tests you should follow the Behat Documentation: Defining steps part.

Now add this line at the end in your “require” block of the composer.json file:

"drupal/drupal-extension":"~3.0"

Then update:

$ composer.phar update

Now if you list the steps definitions you will notice that there are more than the first time, that is because Drupal Extension adds the basics and useful steps that a user will do in a Drupal site. Steps like:

  • “Given I am logged in as a user with the :role role(s)”. This Creates and authenticates a user with the given roles
  • “Given I run cron”. Runs cron
  • “Given I am viewing a/an :type (content )with the title :title. Creates content of the given type.
  • “Then I should see the error message( containing) :message” Checks if the current page contains the given set of error messages.

Installing PhantomJS

Is a headless WebKit scriptable with a JavaScript API. I recommend using PhantomJS because it works well with most javascript libraries.

Node and npm Installation in linux:

$ sudo apt-get install nodejs
$ sudo apt-get install nodejs-legacy
$ sudo apt-get install npm
$ sudo apt-get update

Node and npm Installation in OSX:

$ brew install node

Check the successful installation and their version

$ node -v
$ npm -v

Now install PhantomJS

$ npm install phantomjs

Edit your behat.yml config file and change your selenium2 browser for PhantomJS

javascript:
  selenium2:
    browser: phantomjs

Now apply that config:

$ vendor/bin/behat --config custom-config.yml

To check the installation and its version run:

$ node_modules/phantomjs/bin/phantomjs -v

You can also check the options available by running:

$ node_modules/phantomjs/bin/phantomjs -h

before running your test, in another console tab, run PhantomJS with these options:

$ node_modules/phantomjs/bin/phantomjs --webdriver=4444 debug=true

If the site to test has SSL Certificate, add the options “-ssl-protocol=any –ignore-ssl-errors=true” to that line in order to avoid issues during the test.

Simple scenario example using Drupal Extension

Edit your FeatureContext class inside features directory to extends from the RawDrupalContext class in order to use all the Drupal Extension features.
Then edit your *.feature file or make a new one with that extension and create a simple scenario for testing:

Feature: Content Management
 When I log into the website
 As an administrator
 I should be able to create, edit, and delete page content

 Scenario: An administrative user should be able create page content
 Given I am logged in as a user with the "administrator" role
 When I go to "node/add/page"
 Then I should not see "Access denied"

Save and execute Behat:

$ vendor/bin/behat

The output should look like this:

It says that 1 scenario had been tested and passed also says that 3 steps where tested and passed too. Then the duration of the test.

At this point, after this simple testing you may noticed that the installation is good and everything works. Now you have all you need to test a Drupal site with Behat. Start defining some more steps!

 

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.