Skip to content

Instantly share code, notes, and snippets.

@mikebell
Last active August 29, 2015 14:01
Show Gist options
  • Save mikebell/9469e1f29fff4289ee55 to your computer and use it in GitHub Desktop.
Save mikebell/9469e1f29fff4289ee55 to your computer and use it in GitHub Desktop.
Behat create screenshot on test failure
<?php
use Behat\Mink\Driver\Selenium2Driver;
use Behat\Behat\Event\StepEvent;
/**
* Take screenshot when step fails.
* Works only with Selenium2Driver.
*
* @AfterStep
*/
public function takeScreenshotAfterFailedStep(StepEvent $event)
{
if (StepEvent::FAILED === $event->getResult()) {
$driver = $this->getSession()->getDriver();
if (!($driver instanceof Selenium2Driver)) {
return;
}
$stepNode = $event->getStep();
$scenarioNode = $stepNode->getParent();
$featureNode = $scenarioNode->getFeature();
$directory = '/var/core/';
$file = $featureNode->getTitle() . '-' . $scenarioNode->getTitle();
$screenshot = $driver->getWebDriverSession()->screenshot();
file_put_contents($directory . date('Y-m-d-g-i-s', time()) . '.png', base64_decode($screenshot));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment