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:
This will create a directory features in the behat-demo directory.
Now create a bootstrap file which will include all the necessities for our test.
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';
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:
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.