Skip to content

Instantly share code, notes, and snippets.

@dhargitai
Created June 10, 2014 09:34
Show Gist options
  • Save dhargitai/33673cfd30bce0f4f93a to your computer and use it in GitHub Desktop.
Save dhargitai/33673cfd30bce0f4f93a to your computer and use it in GitHub Desktop.
With this Behat function you can create a screenshot when a test fails.
<?php
/**
* @AfterStep
*/
public function makeScreenshotOnFailure($event)
{
if (StepEvent::FAILED !== $event->getResult() || !($this->getSession()->getDriver() instanceof Selenium2Driver)) {
return;
}
$session = $this->getSession();
$step = $event->getStep();
$fileName = $step->getParent()->getTitle().'.'.$step->getType().' '.$step->getText();
$fileName = preg_replace('/[^a-zA-Z0-9-_\.]/', '_', $fileName).'.png';
$screenshotsDir = $this->kernel->getRootDir().'/../screenshots/';
if (!file_exists($screenshotsDir)) {
mkdir($screenshotsDir);
}
file_put_contents($screenshotsDir.$fileName, $session->getScreenshot());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment