Created
June 10, 2014 09:34
-
-
Save dhargitai/33673cfd30bce0f4f93a to your computer and use it in GitHub Desktop.
With this Behat function you can 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 | |
/** | |
* @AfterStep | |
*/ | |
public function makeScreenshotOnFailure($event) | |
{ | |
if (StepEvent::FAILED !== $event->getResult() || !($this->getSession()->getDriver() instanceof Selenium2Driver)) { | |
return; | |
} | |
$session = $this->getSession(); | |
$step = $event->getStep(); | |
$fileName = $step->getParent()->getTitle().'.'.$step->getType().' '.$step->getText(); | |
$fileName = preg_replace('/[^a-zA-Z0-9-_\.]/', '_', $fileName).'.png'; | |
$screenshotsDir = $this->kernel->getRootDir().'/../screenshots/'; | |
if (!file_exists($screenshotsDir)) { | |
mkdir($screenshotsDir); | |
} | |
file_put_contents($screenshotsDir.$fileName, $session->getScreenshot()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment