Skip to content

Instantly share code, notes, and snippets.

@magickatt
Last active October 25, 2016 15:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save magickatt/3b217d7de01bc8a466f5 to your computer and use it in GitHub Desktop.
Save magickatt/3b217d7de01bc8a466f5 to your computer and use it in GitHub Desktop.
Save screenshot of a failed Behat step
<?php
/**
* @AfterStep
*/
public function takeScreenshotAfterFailedStep($event)
{
if ($event->getTestResult()->getResultCode() === \Behat\Testwork\Tester\Result\TestResult::FAILED) {
$driver = $this->getSession()->getDriver();
if ($driver instanceof \Behat\Mink\Driver\Selenium2Driver) {
$stepText = $event->getStep()->getText();
$fileName = preg_replace('#[^a-zA-Z0-9\._-]#', '', $stepText).'.png';
$filePath = sys_get_temp_dir();
$this->saveScreenshot($fileName, $filePath);
print "Screenshot for '{$stepText}' placed in ".$filePath.DIRECTORY_SEPARATOR.$fileName."\n";
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment