-
-
Save mablae/8cd3e5b0732c88b2a90a to your computer and use it in GitHub Desktop.
Make Behat/Mink create a screenshot when a test fails
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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