-
Notifications
You must be signed in to change notification settings - Fork 394
Add ability to save screenshot and/or dump of html for failed behat tests. #1152
Comments
This is crazy helpful. Here's how I did it (kinda messy, but hey, it works!): You need to add a few things to your # Alongside the other `use` statements.
use Behat\Behat\Hook\Scope\AfterStepScope;
...
class FeatureContext extends RawDrupalContext implements SnippetAcceptingContext {
...
/**
* @AfterStep
*/
public function takeScreenshotAfterFailedStep(AfterStepScope $scope) {
if ($scope->getTestResult()->getResultCode() == 99) {
$this->takeScreenshot();
}
}
/**
* Store a screenshot.
*/
private function takeScreenshot() {
$screenshot = $this->getSession()->getDriver()->getScreenshot();
$path = '/var/www/projectname/' . date('d-m-y') . '-' . uniqid() . '.png';
file_put_contents($path, $screenshot);
print 'Screenshot at: ' . $path;
}
...
} |
This could definitely be brushed up, and we should try to genericize the path a bit. But this is SO helpful compared to having to try to get everything running with Chrome or FireFox on my host computer just to see why a step is failing. In relation to #1134 (comment), I found that the screenshot shows that yes, in fact, there is no text like there should be when rendering under PhantomJS: Not sure why, but the screenshot at least helps me verify that something's incorrect besides just the failed test step. |
Another option: https://packagist.org/packages/bex/behat-screenshot — that's just a Behat Extension that doesn't require any additional FeatureContext code. |
PR posted ^^ |
@geerlingguy Thanks for the contribution! Any change you can add some docs to http://blt.readthedocs.io/en/8.x/readme/testing/#configuration ? |
@grasmash - Will do—I'll add some screenshot config docs. |
PR #1164 |
This would help to understand why a test is failing as behat may be seeing things slightly different compared to your browser.
See:
Behat/Behat#649
https://blogs.library.ucsf.edu/ckm/2014/05/14/see-your-failures-taking-screenshots-with-phantomjs-running-behat-tests/
The text was updated successfully, but these errors were encountered: