Created
May 9, 2013 08:03
-
-
Save mycroft/5546167 to your computer and use it in GitHub Desktop.
Screenshots (and light reports) by Selenium when using Behat and Mink; Only supported by Selenium2/Webdriver
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\Behat\Context\ClosuredContextInterface, | |
Behat\Behat\Context\TranslatedContextInterface, | |
Behat\Behat\Context\BehatContext, | |
Behat\Behat\Exception\PendingException; | |
use Behat\Gherkin\Node\PyStringNode, | |
Behat\Gherkin\Node\TableNode; | |
use Behat\Behat\Event\StepEvent; | |
/** | |
* Features context. | |
*/ | |
class FeatureContext extends Behat\MinkExtension\Context\MinkContext | |
{ | |
/** | |
* Initializes context. | |
* Every scenario gets it's own context object. | |
* | |
* @param array $parameters context parameters (set them up through behat.yml) | |
*/ | |
public function __construct(array $parameters) | |
{ | |
// Initialize your context here | |
} | |
/** | |
* Take screenshot when step fails. | |
* Works only with Selenium2Driver. | |
* | |
* @AfterStep | |
*/ | |
public function takeScreenshotAfterFailedStep($event) | |
{ | |
if (StepEvent::FAILED === $event->getResult()) { | |
$driver = $this->getSession()->getDriver(); | |
if (!($driver instanceof Behat\Mink\Driver\Selenium2Driver)) { | |
return; | |
} | |
$screenshot = $driver->getScreenshot(); | |
$step = $event->getStep(); | |
$scenario = $step->getParent(); | |
$feature = $scenario->getFeature(); | |
$id_str = time() . '_' . rand(1000, 9999); | |
$report_file = "/tmp/report_" . $id_str; | |
$fd = fopen($report_file . ".result", "a+"); | |
fwrite($fd, "File: /tmp/test_" . $id_str . ".png" . "\n"); | |
fwrite($fd, "Date: " . date(DATE_ATOM) . "\n"); | |
fwrite($fd, "File: " . $step->getFile() . " at line " . $step->getLine() . "\n"); | |
fwrite($fd, "Feature: " . $feature->getTitle() . "\n"); | |
fwrite($fd, "Scenario: " . $scenario->getTitle() . "\n"); | |
fwrite($fd, "Step: " . $step->getType() . " " . $step->getText() . "\n"); | |
fclose($fd); | |
file_put_contents($report_file . ".png", $screenshot); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment