Skip to content

Instantly share code, notes, and snippets.

@mablae
Forked from fbrnc/gist:4550079
Created January 26, 2016 02:07
Show Gist options
  • Save mablae/8cd3e5b0732c88b2a90a to your computer and use it in GitHub Desktop.
Save mablae/8cd3e5b0732c88b2a90a 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