Hello everyone!

We have been investing plenty of personal time and energy for many years to share our knowledge with you all. However, we now need your help to keep this blog running. All you have to do is just click one of the adverts on the site, otherwise it will sadly be taken down due to hosting etc. costs. Thank you.

This applies to selenium2 driver.


Behat.yml


default:
context:
class: FeatureContext
parameters:
screen_shot_path: %behat.paths.base%/build/behat/screenshot/
extensions:
Behat\Symfony2Extension\Extension:
mink_driver: true
kernel:
env: test
debug: true
Behat\MinkExtension\Extension:
base_url: 'http://symfony.local/app_test.php/'
files_path: %behat.paths.base%/build/dummy/
javascript_session: selenium2
browser_name: firefox
goutte: ~
selenium2: ~
paths:
features: %behat.paths.base%/src
bootstrap: %behat.paths.features%/Context

FeatureContext.php


namespace Football\TeamBundle\Features\Context;

use Behat\MinkExtension\Context\MinkContext;
use Behat\Mink\Exception\UnsupportedDriverActionException;
use Behat\Mink\Driver\Selenium2Driver;

class FeatureContext extends MinkContext
{
/**
* Where the failure images will be saved.
*
* @var string
*/
protected $screenShotPath;

/**
* Comes from behat.yml file.
*
* @param $parameters
*/
public function __construct(array $parameters)
{
$this->screenShotPath = $parameters['screen_shot_path'];
}

/**
* Take screen-shot when step fails.
* Works only with Selenium2Driver.
*
* @AfterStep
* @param $event
* @throws \Behat\Mink\Exception\UnsupportedDriverActionException
*/
public function takeScreenshotAfterFailedStep($event)
{
if (4 === $event->getResult()) {
$driver = $this->getSession()->getDriver();

if (! ($driver instanceof Selenium2Driver)) {
throw new UnsupportedDriverActionException(
'Taking screen-shots is not supported by %s, use Selenium2Driver instead.',
$driver
);

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

file_put_contents($this->screenShotPath . '/' . $filename, $driver->getScreenshot());
}
}
}