Created
April 9, 2015 00:51
-
-
Save jonathanfranks/0599e5f45a839ccb8248 to your computer and use it in GitHub Desktop.
Behat 3 HTML dump on failure
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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