Commit e42f6c58 authored by xjm's avatar xjm

Issue #2702661 by alexpott, nevergone, GoZ, jibran, dawehner, hgoto, xjm: Make...

Issue #2702661 by alexpott, nevergone, GoZ, jibran, dawehner, hgoto, xjm: Make it simple to take screenshots whilst using JavascriptTestBase
parent 00d0fa72
...@@ -53,4 +53,13 @@ public function testAssertJsCondition() { ...@@ -53,4 +53,13 @@ public function testAssertJsCondition() {
$this->assertJsCondition($javascript, 100); $this->assertJsCondition($javascript, 100);
} }
/**
* Tests creating screenshots.
*/
public function testCreateScreenshot() {
$this->drupalGet('<front>');
$this->createScreenshot('public://screenshot.jpg');
$this->assertFileExists('public://screenshot.jpg');
}
} }
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
namespace Drupal\FunctionalJavascriptTests; namespace Drupal\FunctionalJavascriptTests;
use Drupal\Tests\BrowserTestBase; use Drupal\Tests\BrowserTestBase;
use Zend\Escaper\Escaper;
use Zumba\Mink\Driver\PhantomJSDriver; use Zumba\Mink\Driver\PhantomJSDriver;
/** /**
...@@ -102,6 +103,33 @@ protected function assertJsCondition($condition, $timeout = 1000, $message = '') ...@@ -102,6 +103,33 @@ protected function assertJsCondition($condition, $timeout = 1000, $message = '')
$this->assertTrue($result, $message); $this->assertTrue($result, $message);
} }
/**
* Creates a screenshot.
*
* @param string $filename
* The file name of the resulting screenshot. If using the default phantomjs
* driver then this should be a JPG filename.
* @param string $background_color
* (optional) Background color name. To use the default background color set
* to NULL, however this can result in completely black screenshots if the
* theme does not have a background color. This string is escaped by
* \Zend\Escaper\Escaper::escapeJs().
*
* @throws \Behat\Mink\Exception\UnsupportedDriverActionException
* When operation not supported by the driver.
* @throws \Behat\Mink\Exception\DriverException
* When the operation cannot be done.
*/
protected function createScreenshot($filename, $background_color = 'white') {
$session = $this->getSession();
if (!empty($background_color)) {
$escaper = new Escaper();
$session->executeScript("document.body.style.backgroundColor = '" . $escaper->escapeJs($background_color) . "';");
}
$image = $session->getScreenshot();
file_put_contents($filename, $image);
}
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment