Skip to content

Instantly share code, notes, and snippets.

@jonathanfranks
Created March 21, 2015 21:34
Show Gist options
  • Save jonathanfranks/6a7024037831dad427ce to your computer and use it in GitHub Desktop.
Save jonathanfranks/6a7024037831dad427ce to your computer and use it in GitHub Desktop.
Writing an html dump on a Behat failure
public function recordFailedEvent($event) {
$fileName = $this->timestamp;
// TODO: Make this a setting in behat.yml?
$html_dump_path = 'failures';
$message = '';
$session = $this->getSession();
$page = $session->getPage();
$driver = $session->getDriver();
$date = date('Y-m-d H:i:s');
$url = $session->getCurrentUrl();
$html = $page->getContent();
$event_class = get_class($event);
if (strpos($event_class, 'OutlineExampleEvent') !== FALSE) {
$scenario = $event->getOutline();
} elseif (strpos($event_class, 'ScenarioEvent') !== FALSE) {
$scenario = $event->getScenario();
}
if ($scenario) {
$feature_file_full = $scenario->getFeature()->getFile();
} else {
$feature_file_full = 'failure';
}
$ff = explode('/', $feature_file_full);
$feature_file_name = array_pop($ff);
if (!file_exists($html_dump_path)) {
mkdir($html_dump_path);
}
$html = "<!-- HTML dump from behat \nDate: $date \nUrl: $url -->\n " . $html;
$htmlCapturePath = $html_dump_path . '/' . $fileName . '.' . $feature_file_name . '.html';
file_put_contents($htmlCapturePath, $html);
$message .= "\nHTML available at: " . $htmlCapturePath;
if ($driver instanceof \Behat\Mink\Driver\Selenium2Driver) {
if (!file_exists($html_dump_path)) {
mkdir($html_dump_path);
}
$screenshot = $driver->getScreenshot();
$screenshotFilePath = $html_dump_path . '/' . $fileName . '.png';
file_put_contents($screenshotFilePath, $screenshot);
$message .= "\nScreenshot available at: " . $screenshotFilePath;
}
print $message . PHP_EOL;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment