diff --git a/core/modules/simpletest/tests/src/FunctionalJavascript/BrowserWithJavascriptTest.php b/core/modules/simpletest/tests/src/FunctionalJavascript/BrowserWithJavascriptTest.php index 572a842f5b9474f843e7f5e14a1508f684ab0311..24397990e041e81ce137ed2664bede06732f9db1 100644 --- a/core/modules/simpletest/tests/src/FunctionalJavascript/BrowserWithJavascriptTest.php +++ b/core/modules/simpletest/tests/src/FunctionalJavascript/BrowserWithJavascriptTest.php @@ -53,4 +53,13 @@ public function testAssertJsCondition() { $this->assertJsCondition($javascript, 100); } + /** + * Tests creating screenshots. + */ + public function testCreateScreenshot() { + $this->drupalGet('<front>'); + $this->createScreenshot('public://screenshot.jpg'); + $this->assertFileExists('public://screenshot.jpg'); + } + } diff --git a/core/tests/Drupal/FunctionalJavascriptTests/JavascriptTestBase.php b/core/tests/Drupal/FunctionalJavascriptTests/JavascriptTestBase.php index 116bd3256384863c028ce529dbfe51ba10ce3da6..eea62d1e20b0fdb752b49d2557c76ac6aba1e188 100644 --- a/core/tests/Drupal/FunctionalJavascriptTests/JavascriptTestBase.php +++ b/core/tests/Drupal/FunctionalJavascriptTests/JavascriptTestBase.php @@ -102,6 +102,30 @@ protected function assertJsCondition($condition, $timeout = 1000, $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 bool $set_background_color + * (optional) By default this method will set the background color to white. + * Set to FALSE to override this behaviour. + * + * @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, $set_background_color = TRUE) { + $session = $this->getSession(); + if ($set_background_color) { + $session->executeScript("document.body.style.backgroundColor = 'white';"); + } + $image = $session->getScreenshot(); + file_put_contents($filename, $image); + } + /** * {@inheritdoc} */