Skip to content
This repository was archived by the owner on Mar 5, 2025. It is now read-only.

Add ability to save screenshot and/or dump of html for failed behat tests. #1152

Closed
lahoosascoots opened this issue Mar 7, 2017 · 7 comments
Labels
Enhancement A feature or feature request

Comments

@lahoosascoots
Copy link
Contributor

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/

@geerlingguy
Copy link
Contributor

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 FeatureContext (in tests/behat/features/bootstrap/Drupal/FeatureContext.php):

# 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;
  }

  ...

}

@geerlingguy
Copy link
Contributor

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:

07-03-17-58bee5c652695

Not sure why, but the screenshot at least helps me verify that something's incorrect besides just the failed test step.

@geerlingguy
Copy link
Contributor

Another option: https://packagist.org/packages/bex/behat-screenshot — that's just a Behat Extension that doesn't require any additional FeatureContext code.

@geerlingguy
Copy link
Contributor

PR posted ^^

grasmash pushed a commit that referenced this issue Mar 9, 2017
* Fixes #1152: Add bex/behat-screenshot for screenshots on failures.

* Issue #1152: Add Imagemagick to Drupal VM config for composite screenshots.
@grasmash
Copy link
Contributor

grasmash commented Mar 9, 2017

@geerlingguy Thanks for the contribution! Any change you can add some docs to http://blt.readthedocs.io/en/8.x/readme/testing/#configuration ?

@geerlingguy
Copy link
Contributor

@grasmash - Will do—I'll add some screenshot config docs.

@geerlingguy
Copy link
Contributor

PR #1164

@grasmash grasmash added the Enhancement A feature or feature request label Mar 14, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Enhancement A feature or feature request
Projects
None yet
Development

No branches or pull requests

3 participants