Add this definition to your FeatureContext.php to take a screenshot of failed steps for your Behat/Selenium2 tests. It creates a directory called "screenshots" in your project directory (if one doesn't already exist) and within it places screenshots whose filenames consist of the date and time as well as the name of the failed step.
/**
* @AfterStep
*/
public function takeScreenShotAfterFailedStep(afterStepScope $scope)
{
if (99 === $scope->getTestResult()->getResultCode()) {
$driver = $this->getSession()->getDriver();
$filePath = getcwd() . DIRECTORY_SEPARATOR . 'screenshots';
if (!file_exists($filePath)) {
mkdir($filePath);
}
$stepText = $scope->getStep()->getText();
$fileTitle = date('Y-m-d--H:i:s') . '--' . preg_replace("#[^a-zA-Z0-9\._-]#", '', $stepText);
$fileName = $filePath . DIRECTORY_SEPARATOR . $fileTitle . '.png';
$screenshot = $this->getSession()->getDriver()->getScreenshot();
file_put_contents($fileName, $screenshot);
print "Screenshot for '{$stepText}' placed in {$fileName}\n";
}
}