Features
< Back to Blog Overview

Behat and Mink webdriver testing

2012-02-10
  • Share on Facebook
  • Share on Twitter
  • Share on LinkedIn
  • Share on HackerNews

If you haven't heard of Behat yet, it's a BDD framework which runs on PHP written by Konstantin Kudryashov. Behat is similar to Cucumber for Ruby, Lettuce for Python and SpecFlow for .NET.


You'll use Behat for functional testing, data-driven testing and API testing. With Behat you can easily write Gherkin stories (which are human readable test cases).


To start using Selenium together with Behat, you'll need Mink. Mink is perfect for functional testing together with Behat. It includes Selenium drivers which will run on our Selenium grid.


We've added a demo project which is ready to be used in our Github repo.

Installing Behat and Mink

Make sure you have PEAR installed. You need PEAR to install Behat and its dependencies.

$ sudo pear channel-discover pear.behat.org
	$ sudo pear channel-discover pear.symfony.com
	$ sudo pear install behat/gherkin-beta
	$ sudo pear install behat/behat-beta

Verify your installation: $ behat --version


Now we need to install Mink: $ pear install behat/mink


In order to use our TestingBot.com grid, we need to install the PHP TestingBot connector:

$ sudo pear channel-discover testingbot.github.com/pear
	$ sudo pear install -a testingbot/TestingBot

Set up a new project

Create a new directory and initialise Behat:

$ mkdir behat-demo
 $ cd behat-demo
 $ behat --init

This will create a directory features in the behat-demo directory.

$ cd features/bootstrap

Now create a bootstrap file which will include all the necessities for our test.

$ vim bootstrap.php

Add the following in your bootstrap file:


 <?php
 date_default_timezone_set('Europe/London');
 require_once 'mink/autoload.php';
 require_once 'PHPUnit/Autoload.php';
 require_once 'PHPUnit/Framework/Assert/Functions.php';
 require_once 'PHPUnit/Extensions/SeleniumTestCase.php';
 require_once 'PHPUnit/Extensions/TestingBotTestCase.php';
Specify the correct settings in your behat.yml file:
default:

    context:

        parameters:

            javascript_session: selenium

            base_url: http://www.google.com

            browser: iexplore

            show_cmd: open %s

            selenium:

              host: hub.testingbot.com

              port: 4444

              browser: >

                {

                  "client_key" : "[KEY]",

                  "client_secret" : "[SECRET]",

                  "browserName" : "iexplore",

                  "version" : 8

                }

Don't forget to add your client_key and client_secret, which you can obtain from our member area.


Next, make sure the FeatureContext.php file contains the following: $ vim features/bootstrap/FeatureContext.php

<?php

use Behat\Behat\Context\ClosuredContextInterface,
    Behat\Behat\Context\TranslatedContextInterface,
    Behat\Behat\Context\BehatContext,
    Behat\Behat\Exception\PendingException;
use Behat\Gherkin\Node\PyStringNode,
    Behat\Gherkin\Node\TableNode;
use Behat\Mink\Behat\Context\MinkContext;
use Behat\Mink\Session;
use Behat\Mink\Driver\DriverInterface;

require_once 'bootstrap.php';

class FeatureContext extends MinkContext

Make sure FeatureContext extends from MinkContext.

Write a feature

We'll now write a feature which will perform a basic test. The feature we'll use:

$ vim googleSearch

 Feature: googleSearch
  In order to search information on google
  As a user
  I want to get sensible results from site

 @javascript
  Scenario Outline: Search Keywords on Google
    Given I am on "/"
    And I fill in searchBox with "<input>"
    When I press submit button
    Then I should see "<output>" 

    Examples:
      | input           | output          |                                   
      | TestingBot      | Selenium        |

This feature is written in Gherkin DSL (DSL meaning Domain Specific Language). This feature will have the following steps:

  • A user navigates to the homepage of Google
  • The user enters TestingBot in the search field
  • The user clicks the submit button (search button)
  • TestingBot should be included in the results

You can run this test by executing the following command: $ behat --name googleSearch


After executing the command, Behat will return some step suggestions which we'll need to add to the FeatureContext.php file. The suggestions will be:

/**
     * @Given /^I fill in searchBox with "([^"]*)"$/
     */
    public function iFillInSearchboxWith($argument1)
    {
        throw new PendingException();
    }

    /**
     * @When /^I press submit button$/
     */
    public function iPressSubmitButton()
    {
        throw new PendingException();
    }

Add the following to your FeatureContext.php file:

/**
     * @Given /^I fill in searchBox with "([^"]*)"$/
     */
    public function iFillInSearchboxWith($argument1)
    {
        $this->fillField("q",$argument1);
    }

    /**
     * @When /^I press submit button$/
     */
    public function iPressSubmitButton()
    {
        $this->getMink()->getSession()->getDriver()->click("//input[@type='submit'][1]"); //first submit button = search button on Google

        $this->getMink()->getSession()->wait("3000");
    }

Now run your feature again: $ behat --name googleSearch


The test should succeed and you will see:

1 scenario (1 passed)
4 steps (4 passed)

We hope this was a helpful post and helped you as an introduction in using Behat and Mink together with our Selenium grid. You can download this demo project from our Github repo.

TestingBot Logo

Sign up for a Free Trial

Start testing your apps with TestingBot.

No credit card required.

Other Articles

Selenium cloud testing with Jenkins

Jenkins (formerly known as Hudson) is an open-sourced continuous integration (CI) system built in Java. With Jenkins you are able to run your Selen...

Read more
Browser statistics gathered from Selenium testing

This week we've reached the 35,000 tests milestone so we thought it might be interesting to do some research in the statistics we've gathered durin...

Read more
Video Record your Selenium Tests

When a Selenium test fails, you usually get a clear error message or trace to indicate where the problem is located.&nbsp;A video of your Seleniu...

Read more
Mobile Testing with Selenium and Android Ice Cream Sandwich

Today we've added Android to our Selenium grid, which means you can now use our grid for mobile testing as well. We've hooked up an Android Ice C...

Read more
Receive SMS alerts when a Selenium test fails

Today we've added SMS support to our alert options. If a test fails, we can now alert you via e-mail, push notification and SMS.

Read more
Selenium automated browser testing with C# and NUnit.

In this blog post we'll highlight how easy it is to start testing websites with .NET framework 4 or .NET framework 3 and C#. NUnit is built for all...

Read more