Skip to content

Instantly share code, notes, and snippets.

@jonathanfranks
Created April 9, 2015 00:51
Show Gist options
  • Save jonathanfranks/0599e5f45a839ccb8248 to your computer and use it in GitHub Desktop.
Save jonathanfranks/0599e5f45a839ccb8248 to your computer and use it in GitHub Desktop.
Behat 3 HTML dump on failure
<?php
use Behat\MinkExtension\Context\RawMinkContext,
Behat\Behat\Hook\Scope\AfterScenarioScope;
/**
* Defines application features from the specific context.
*/
class FailureHtmlDumpContext extends RawMinkContext {
/**
* Initializes context.
*
* Every scenario gets its own context instance.
* You can also pass arbitrary arguments to the
* context constructor through behat.yml.
*/
public function __construct() {
}
/**
* @AfterScenario
*/
public function takeScreenshotAfterFailedStep(AfterScenarioScope $scope) {
if (99 === $scope->getTestResult()->getResultCode()) {
$filename = $scope->getFeature()->getFile() . '-' . $scope->getScenario()->getLine();
$this->takeScreenshot($filename);
}
}
private function takeScreenshot($scenarioFileName) {
$filePieces = explode('/', $scenarioFileName);
$fileName = array_pop($filePieces) . '-' . time() . '.html';
array_pop($filePieces);
$html_dump_path = implode('/', $filePieces) . '/failures';
if (!file_exists($html_dump_path)) {
mkdir($html_dump_path);
}
$html = $this->getSession()->getPage()->getContent();
$htmlCapturePath = $html_dump_path . '/' . $fileName;
file_put_contents($htmlCapturePath, $html);
$message = "\nHTML available at: " . $htmlCapturePath;
print $message . PHP_EOL;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment