Skip to content

Instantly share code, notes, and snippets.

@fbrnc
Forked from michalochman/gist:3175175
Last active September 25, 2017 11:53
Show Gist options
  • Save fbrnc/4550079 to your computer and use it in GitHub Desktop.
Save fbrnc/4550079 to your computer and use it in GitHub Desktop.
Make Behat/Mink create a screenshot when a test fails
<?php
class FeatureContext extends MinkContext {
/**
* Take screenshot when step fails.
* Works only with Selenium2Driver.
*
* @AfterStep
*/
public function takeScreenshotAfterFailedStep(Behat\Behat\Event\StepEvent $event) {
if (Behat\Behat\Event\StepEvent::FAILED === $event->getResult()) {
$driver = $this->getSession()->getDriver();
if ($driver instanceof Behat\Mink\Driver\Selenium2Driver) {
$step = $event->getStep();
$id = $step->getParent()->getTitle() . '.' . $step->getType() . ' ' . $step->getText();
$fileName = 'Fail.'.preg_replace('/[^a-zA-Z0-9-_\.]/','_', $id) . '.jpg';
file_put_contents($fileName, $driver->getScreenshot());
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment