Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Take screenshot with Behat/Mink after failed step
/**
* 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));
}
}
@tarjei

This comment has been minimized.

Copy link

commented Jun 6, 2013

You need to use
getWebDriverSession()
instead of ->wdSession

@umpirsky

This comment has been minimized.

Copy link

commented Oct 14, 2013

Better:

$this->getSession()->getDriver()->getScreenshot();

Its already base64_decoded.

@stoefln

This comment has been minimized.

Copy link

commented Dec 26, 2014

Min version requirement for behat is 2.5 BTW. Just found out...

@MKorostoff

This comment has been minimized.

Copy link

commented Apr 16, 2015

working with behat 3.0:

use Behat\Mink\Driver\Selenium2Driver;
use Behat\Behat\Hook\Scope\AfterStepScope;

  /**
   * @AfterStep
   */
  public function takeScreenShotAfterFailedStep(afterStepScope $scope)
  {
    if (99 === $scope->getTestResult()->getResultCode()) {
      $driver = $this->getSession()->getDriver();
      if (!($driver instanceof Selenium2Driver)) {
        return;
      }
      file_put_contents('/tmp/test.png', $this->getSession()->getDriver()->getScreenshot());
    }
  }

Note, error code is changed to 99 and the getScreenshot method now returns a binary file, rather than a base64 encoded string.

@frankcarey

This comment has been minimized.

Copy link

commented Oct 27, 2015

This is also automatically supported by our behat 3.x extension, amongst other useful features.
https://github.com/devinci-code/devinci-behat-extension

@elvetemedve

This comment has been minimized.

Copy link

commented May 27, 2016

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

@AlexSkrypnyk

This comment has been minimized.

Copy link

commented May 26, 2017

Another dedicated extension with support for Mink (Goutte) and Selenium screenshots
https://github.com/integratedexperts/behat-screenshot

@forceedge01

This comment has been minimized.

Copy link

commented Sep 12, 2018

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
You can’t perform that action at this time.