diff --git a/core/modules/simpletest/tests/src/FunctionalJavascript/BrowserWithJavascriptTest.php b/core/modules/simpletest/tests/src/FunctionalJavascript/BrowserWithJavascriptTest.php index 572a842..2439799 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(''); + $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 116bd32..2c2ac3a 100644 --- a/core/tests/Drupal/FunctionalJavascriptTests/JavascriptTestBase.php +++ b/core/tests/Drupal/FunctionalJavascriptTests/JavascriptTestBase.php @@ -103,6 +103,29 @@ protected function assertJsCondition($condition, $timeout = 1000, $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. + * + * @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)) { + $session->executeScript("document.body.style.backgroundColor = '" . $background_color . "';"); + } + $image = $session->getScreenshot(); + file_put_contents($filename, $image); + } + + /** * {@inheritdoc} */ public function assertSession($name = NULL) {