Created
June 19, 2019 21:04
-
-
Save xurizaemon/9a2d030a0ef22f173b36727532691ead to your computer and use it in GitHub Desktop.
drupal screenshot on test 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
<?php | |
// Extracted from FeatureContext.php in Behat. | |
// This may also require a Behat patch? | |
/** @AfterStep */ | |
public function takeScreenshotOnFail(Behat\Behat\Hook\Scope\AfterStepScope $scope) { | |
if (99 === $scope->getTestResult()->getResultCode()) { | |
$this->takeScreenshot(true); | |
} | |
} | |
/** | |
* Take a screenshot of the current page. | |
* | |
* Images for Goutte require X server & wkhtmlto*, so disabled here, and | |
* dump .html instead. | |
* | |
* @Given /^I take a screenshot$/ | |
*/ | |
public function takeScreenshot($fail = FALSE) { | |
// @see https://gist.github.com/Quilted/1ac94e717b50c486b3ed | |
// @see https://github.com/Behat/Behat/issues/649 | |
$driver = $this->getSession()->getDriver(); | |
$filepath = $this->getMinkParameter('files_path' ) . '/debug'; | |
if ($fail) { | |
$filename = sprintf('%s_%s_%s-FAIL', $this->getMinkParameter('browser_name'), date('c'), uniqid('', true)); | |
} | |
else { | |
$filename = sprintf('%s_%s_%s-DEBUG', $this->getMinkParameter('browser_name'), date('c'), uniqid('', true)); | |
} | |
if (method_exists($driver, 'saveScreenshot')) { | |
$this->saveScreenshot($filename . '.png', $filepath); | |
} | |
else { | |
switch (get_class($driver)) { | |
case 'Behat\Mink\Driver\GoutteDriver': | |
$html = $this->getSession()->getDriver()->getContent(); | |
// @todo: process file to ensure all links/images have full domain... | |
$url = $this->getSession()->getCurrentUrl(); | |
$scheme = parse_url($url, PHP_URL_SCHEME); | |
$host = parse_url($url, PHP_URL_HOST); | |
$html = str_replace('src="/', 'src="' . $scheme . '://' . $host . '/', $html); | |
// $filepath = $filepath ? $filepath : (ini_get('upload_tmp_dir') ? ini_get('upload_tmp_dir') : sys_get_temp_dir()); | |
$file_markup = $filepath . '/' . $filename . '.html'; | |
file_put_contents($file_markup, $html); | |
// Generate screenshot. | |
$file_image = $filepath . '/' . $filename . '.png'; | |
// exec("wkhtmltoimage $file_markup $file_image"); | |
break; | |
} | |
} | |
print 'Screenshot at: ' . $file_markup . PHP_EOL; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment