-
-
Save stmllr/5430470 to your computer and use it in GitHub Desktop.
Take screenshot after a step failed in Behat/Mink using Selenium WebDriver
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. | |
* Screenshot is saved at [Date]/[Feature]/[Scenario]/[Step].jpg | |
* | |
* @AfterStep @javascript | |
*/ | |
public function takeScreenshotAfterFailedStep(Behat\Behat\Event\StepEvent $event) { | |
if ($event->getResult() === Behat\Behat\Event\StepEvent::FAILED) { | |
$driver = $this->getSession()->getDriver(); | |
if ($driver instanceof Behat\Mink\Driver\Selenium2Driver) { | |
$step = $event->getStep(); | |
$path = array( | |
'date' => date("Ymd-Hi"), | |
'feature' => $step->getParent()->getFeature()->getTitle(), | |
'scenario' => $step->getParent()->getTitle(), | |
'step' => $step->getType() . ' ' . $step->getText() | |
); | |
$path = preg_replace('/[^\-\.\w]/', '_', $path); | |
$filename = '/tmp/behat/' . implode('/', $path) . '.jpg'; | |
// Create directories if needed | |
if (!@is_dir(dirname($filename))) { | |
@mkdir(dirname($filename), 0775, TRUE); | |
} | |
file_put_contents($filename, $driver->getScreenshot()); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Does not seem to work anymore. Adding the function to the FeatureContext didnt hook it up with an afterStep. Has anything changed recently? Is this still working for you?