Last active
August 29, 2015 14:13
-
-
Save jonathanfranks/552b6f650e15f6dfe113 to your computer and use it in GitHub Desktop.
Behat html dump on failure
This file contains 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
public function afterScenario($event) { | |
if ($event->getResult()) { | |
$this->recordFailedEvent($event); | |
} | |
parent::afterScenario($event); | |
} | |
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