Andrew Cavanagh

Developer.  Project Architect.  Huge Nerd.

(not necessarily in that order)

Quick code snippet to add to your context for capturing a screenshot on any failed step:

 * Take screenshot when step fails.
 * Works only with Selenium2Driver.
 * @AfterStep
public function takeScreenshotAfterFailedStep(StepEvent $event)
    if (StepEvent::FAILED === $event->getResult()) {
        $driver = $this->getSession()->getDriver();

    //This only works with the selenium2 driver, if you're using goutte for some tests you may want to add something like this:
    //    if (!($driver instanceof Selenium2Driver)) {
    //        var_dump('driver not found');
    //        return;
    //    }

        $stepNode = $event->getStep();
        $scenarioNode = $stepNode->getParent();
        $featureNode = $scenarioNode->getFeature();

         $title = sprintf('failed_%s_%s_%s.png', $stepNode->getText(), $scenarioNode->getTitle(), $featureNode->getTitle());
        $screenshot = $driver->getWebDriverSession()->screenshot();
        file_put_contents('/path/to/screenshots/' . $title, base64_decode($screenshot));

This uses Behat’s built in @AfterStep hook, so including it in any active feature context will cause it to run after every step. See for documentation on hooks.

comments powered by Disqus