This applies to selenium2 driver.


Folder structure


football
build
phing
behat
screenshots

Composer.json


"require-dev": {
"behat/behat" : "3.0.15",
"behat/symfony2-extension" : "2.0.0",
"behat/mink": "1.6.1",
"behat/mink-extension": "2.0.1",
"behat/mink-browserkit-driver": "1.2.0",
"behat/mink-goutte-driver": "1.1.0",
"behat/mink-selenium2-driver": "1.2.0"
}

Behat.yml


default:
extensions:
Behat\Symfony2Extension: ~
Behat\MinkExtension:
base_url: http://football.local/app_test.php
browser_name: firefox
sessions:
goutte: # fast, CLI, browser, no javascript support
goutte: ~
selenium2: # fast, CLI, opens up a browser
selenium2: ~
symfony2: # bleeding fast, CLI, no browser
symfony2: ~
suites:
backend:
type: symfony_bundle
bundle: ApplicationBackendBundle
mink_session: selenium2
contexts:
- Application\BackendBundle\Features\Context\FeatureContext:
screen_shot_path: build/phing/behat/screenshot

frontend:
type: symfony_bundle
bundle: ApplicationFrontendBundle
mink_session: selenium2
contexts:
- Application\FrontendBundle\Features\Context\FeatureContext:
screen_shot_path: build/phing/behat/screenshot

FeatureContext.php


namespace Application\BackendBundle\Features\Context;

use Behat\Behat\Hook\Scope\AfterStepScope;
use Behat\Mink\Driver\Selenium2Driver;
use Behat\MinkExtension\Context\MinkContext;

class FeatureContext extends MinkContext
{
private $screenShotPath;

public function __construct($screen_shot_path)
{
$this->screenShotPath = $screen_shot_path;
}

/**
* Take screen-shot when step fails. Works only with Selenium2Driver.
*
* @AfterStep
* @param AfterStepScope $scope
*/
public function takeScreenshotAfterFailedStep(AfterStepScope $scope)
{
if (99 === $scope->getTestResult()->getResultCode()) {
$driver = $this->getSession()->getDriver();

if (! $driver instanceof Selenium2Driver) {
return;
}

if (! is_dir($this->screenShotPath)) {
mkdir($this->screenShotPath, 0777, true);
}

$filename = sprintf(
'%s_%s_%s.%s',
$this->getMinkParameter('browser_name'),
date('Ymd') . '-' . date('His'),
uniqid('', true),
'png'
);

$this->saveScreenshot($filename, $this->screenShotPath);
}
}
}

Selenium Standalone Server


Download it from here, save it into your project folder and run it with command below.


# If you want to use firefox:
java -jar selenium-server-standalone-2.43.1.jar
# If you want to use chromedriver (chromedriver has Linux and Mac versions):
java -jar selenium-server-standalone-2.43.1.jar -Dwebdriver.chrome.driver="chromedriver"

Run the test


bin/behat --suite=backend