TestingBot Blog

TestingBot provides a cloud of browsers to run Automated, Live and Visual browser testing.

< Back to Blog Overview

Selenium testing with Behat and Mink


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.

Alright enough introduction, let's see some code examples.
[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 initialize 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:

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:

javascript_session: selenium
base_url: http://www.google.com
browser: iexplore
show_cmd: open %s
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


use Behat\Behat\Context\ClosuredContextInterface,
use Behat\Gherkin\Node\PyStringNode,
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

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

| 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)

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

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.

Questions? Please leave a comment.

< Back to Blog Overview