Last active
August 29, 2015 14:01
-
-
Save mikebell/9469e1f29fff4289ee55 to your computer and use it in GitHub Desktop.
Behat create screenshot on test failure
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 | |
use Behat\Mink\Driver\Selenium2Driver; | |
use Behat\Behat\Event\StepEvent; | |
/** | |
* Take screenshot when step fails. | |
* Works only with Selenium2Driver. | |
* | |
* @AfterStep | |
*/ | |
public function takeScreenshotAfterFailedStep(StepEvent $event) | |
{ | |
if (StepEvent::FAILED === $event->getResult()) { | |
$driver = $this->getSession()->getDriver(); | |
if (!($driver instanceof Selenium2Driver)) { | |
return; | |
} | |
$stepNode = $event->getStep(); | |
$scenarioNode = $stepNode->getParent(); | |
$featureNode = $scenarioNode->getFeature(); | |
$directory = '/var/core/'; | |
$file = $featureNode->getTitle() . '-' . $scenarioNode->getTitle(); | |
$screenshot = $driver->getWebDriverSession()->screenshot(); | |
file_put_contents($directory . date('Y-m-d-g-i-s', time()) . '.png', base64_decode($screenshot)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment