Forked from michaelesmith/takeScreenshotAfterFailedStep.php
Last active
August 29, 2015 14:07
-
-
Save adfinlay/28ee3613711a03e0f4aa to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
*/ | |
public function takeScreenshotAfterFailedStep(StepEvent $event) { | |
if ($event->getResult() === StepEvent::FAILED) { | |
$driver = $this->getSession()->getDriver(); | |
$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); | |
$imgBase = $this->kernel->getContainer()->getParameter('behat_screenshot_dir') ?: '/tmp/behat'; | |
$htmlBase = $this->kernel->getContainer()->getParameter('behat_response_dir') ?: '/tmp/behat'; | |
$filename = implode(':', $path); | |
printf('Saving response: "%s"', $filename); | |
// Create directories if needed | |
if (!@is_dir($htmlBase)) { | |
@mkdir($htmlBase, 0775, TRUE); | |
} | |
if (!@is_dir($imgBase)) { | |
@mkdir($imgBase, 0775, TRUE); | |
} | |
file_put_contents($htmlBase . '/' . $filename. '.html', $this->getSession()->getPage()->getContent()); | |
if ($driver instanceof Selenium2Driver) { | |
try{ | |
file_put_contents($imgBase . '/' . $filename. '.png', $driver->getScreenshot()); | |
}catch (\Exception $e) { | |
pritnf('Unable to save image'); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment