Created
July 25, 2012 08:53
-
-
Save michalochman/3175175 to your computer and use it in GitHub Desktop.
Take screenshot with Behat/Mink after failed step
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
/** | |
* Take screenshot when step fails. | |
* Works only with Selenium2Driver. | |
* | |
* @AfterStep | |
*/ | |
public function takeScreenshotAfterFailedStep($event) | |
{ | |
if (4 === $event->getResult()) { | |
$driver = $this->getSession()->getDriver(); | |
if (!($driver instanceof Selenium2Driver)) { | |
//throw new UnsupportedDriverActionException('Taking screenshots is not supported by %s, use Selenium2Driver instead.', $driver); | |
return; | |
} | |
$screenshot = $driver->wdSession->screenshot(); | |
file_put_contents('/tmp/test.png', base64_decode($screenshot)); | |
} | |
} |
Another dedicated extension with support for Mink (Goutte) and Selenium screenshots
https://github.com/integratedexperts/behat-screenshot
Another extension that gives more than just a screenshot with minimal setup! https://github.com/forceedge01/behat-fail-aid
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
There is a dedicated Behat extension (bex/behat-screenshot) for taking screenshots, which can be installed via Composer easily.
It can upload the created image to public image hosting sites and display the URL to it on the console. If you need to upload images to a private server, you can create an image adapter for that easily.
Screenshot taking mode allows to make a combined image of previous steps, which can come handy if one of the previous step is responsible for the failing test case.
If you interested, have a look here:
https://github.com/elvetemedve/behat-screenshot