annotate core/tests/Drupal/FunctionalJavascriptTests/JavascriptTestBase.php @ 14:1fec387a4317

Update Drupal core to 8.5.2 via Composer
author Chris Cannam
date Mon, 23 Apr 2018 09:46:53 +0100
parents 4c8ae668cc8c
children c2387f117808
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\FunctionalJavascriptTests;
Chris@0 4
Chris@0 5 use Drupal\Tests\BrowserTestBase;
Chris@0 6 use Zumba\GastonJS\Exception\DeadClient;
Chris@0 7 use Zumba\Mink\Driver\PhantomJSDriver;
Chris@0 8
Chris@0 9 /**
Chris@0 10 * Runs a browser test using a driver that supports Javascript.
Chris@0 11 *
Chris@0 12 * Base class for testing browser interaction implemented in JavaScript.
Chris@0 13 */
Chris@0 14 abstract class JavascriptTestBase extends BrowserTestBase {
Chris@0 15
Chris@0 16 /**
Chris@0 17 * {@inheritdoc}
Chris@14 18 *
Chris@14 19 * To use a webdriver based approach, please use DrupalSelenium2Driver::class.
Chris@14 20 * We will switch the default later.
Chris@0 21 */
Chris@0 22 protected $minkDefaultDriverClass = PhantomJSDriver::class;
Chris@0 23
Chris@0 24 /**
Chris@0 25 * {@inheritdoc}
Chris@0 26 */
Chris@0 27 protected function initMink() {
Chris@14 28 if ($this->minkDefaultDriverClass === DrupalSelenium2Driver::class) {
Chris@14 29 $this->minkDefaultDriverArgs = ['chrome', NULL, 'http://localhost:4444/'];
Chris@14 30 }
Chris@14 31 elseif ($this->minkDefaultDriverClass === PhantomJSDriver::class) {
Chris@14 32 // Set up the template cache used by the PhantomJS mink driver.
Chris@14 33 $path = $this->tempFilesDirectory . DIRECTORY_SEPARATOR . 'browsertestbase-templatecache';
Chris@14 34 $this->minkDefaultDriverArgs = [
Chris@14 35 'http://127.0.0.1:8510',
Chris@14 36 $path,
Chris@14 37 ];
Chris@14 38 if (!file_exists($path)) {
Chris@14 39 mkdir($path);
Chris@14 40 }
Chris@0 41 }
Chris@0 42
Chris@0 43 try {
Chris@0 44 return parent::initMink();
Chris@0 45 }
Chris@0 46 catch (DeadClient $e) {
Chris@0 47 $this->markTestSkipped('PhantomJS is either not installed or not running. Start it via phantomjs --ssl-protocol=any --ignore-ssl-errors=true vendor/jcalderonzumba/gastonjs/src/Client/main.js 8510 1024 768&');
Chris@0 48 }
Chris@0 49 catch (\Exception $e) {
Chris@0 50 $this->markTestSkipped('An unexpected error occurred while starting Mink: ' . $e->getMessage());
Chris@0 51 }
Chris@0 52 }
Chris@0 53
Chris@0 54 /**
Chris@0 55 * {@inheritdoc}
Chris@0 56 */
Chris@0 57 protected function tearDown() {
Chris@0 58 if ($this->mink) {
Chris@0 59 // Wait for all requests to finish. It is possible that an AJAX request is
Chris@0 60 // still on-going.
Chris@0 61 $result = $this->getSession()->wait(5000, '(typeof(jQuery)=="undefined" || (0 === jQuery.active && 0 === jQuery(\':animated\').length))');
Chris@0 62 if (!$result) {
Chris@0 63 // If the wait is unsuccessful, there may still be an AJAX request in
Chris@0 64 // progress. If we tear down now, then this AJAX request may fail with
Chris@0 65 // missing database tables, because tear down will have removed them.
Chris@0 66 // Rather than allow it to fail, throw an explicit exception now
Chris@0 67 // explaining what the problem is.
Chris@0 68 throw new \RuntimeException('Unfinished AJAX requests while tearing down a test');
Chris@0 69 }
Chris@0 70 }
Chris@0 71 parent::tearDown();
Chris@0 72 }
Chris@0 73
Chris@0 74 /**
Chris@14 75 * {@inheritdoc}
Chris@14 76 */
Chris@14 77 protected function getMinkDriverArgs() {
Chris@14 78 if ($this->minkDefaultDriverClass === DrupalSelenium2Driver::class) {
Chris@14 79 return getenv('MINK_DRIVER_ARGS_WEBDRIVER') ?: getenv('MINK_DRIVER_ARGS_PHANTOMJS') ?: parent::getMinkDriverArgs();
Chris@14 80 }
Chris@14 81 elseif ($this->minkDefaultDriverClass === PhantomJSDriver::class) {
Chris@14 82 return getenv('MINK_DRIVER_ARGS_PHANTOMJS') ?: parent::getMinkDriverArgs();
Chris@14 83 }
Chris@14 84 return parent::getMinkDriverArgs();
Chris@14 85 }
Chris@14 86
Chris@14 87 /**
Chris@0 88 * Asserts that the element with the given CSS selector is visible.
Chris@0 89 *
Chris@0 90 * @param string $css_selector
Chris@0 91 * The CSS selector identifying the element to check.
Chris@0 92 * @param string $message
Chris@0 93 * Optional message to show alongside the assertion.
Chris@0 94 *
Chris@0 95 * @deprecated in Drupal 8.1.x, will be removed before Drupal 8.3.x. Use
Chris@0 96 * \Behat\Mink\Element\NodeElement::isVisible() instead.
Chris@0 97 */
Chris@0 98 protected function assertElementVisible($css_selector, $message = '') {
Chris@0 99 $this->assertTrue($this->getSession()->getDriver()->isVisible($this->cssSelectToXpath($css_selector)), $message);
Chris@0 100 }
Chris@0 101
Chris@0 102 /**
Chris@0 103 * Asserts that the element with the given CSS selector is not visible.
Chris@0 104 *
Chris@0 105 * @param string $css_selector
Chris@0 106 * The CSS selector identifying the element to check.
Chris@0 107 * @param string $message
Chris@0 108 * Optional message to show alongside the assertion.
Chris@0 109 *
Chris@0 110 * @deprecated in Drupal 8.1.x, will be removed before Drupal 8.3.x. Use
Chris@0 111 * \Behat\Mink\Element\NodeElement::isVisible() instead.
Chris@0 112 */
Chris@0 113 protected function assertElementNotVisible($css_selector, $message = '') {
Chris@0 114 $this->assertFalse($this->getSession()->getDriver()->isVisible($this->cssSelectToXpath($css_selector)), $message);
Chris@0 115 }
Chris@0 116
Chris@0 117 /**
Chris@0 118 * Waits for the given time or until the given JS condition becomes TRUE.
Chris@0 119 *
Chris@0 120 * @param string $condition
Chris@0 121 * JS condition to wait until it becomes TRUE.
Chris@0 122 * @param int $timeout
Chris@0 123 * (Optional) Timeout in milliseconds, defaults to 10000.
Chris@0 124 * @param string $message
Chris@0 125 * (optional) A message to display with the assertion. If left blank, a
Chris@0 126 * default message will be displayed.
Chris@0 127 *
Chris@0 128 * @throws \PHPUnit_Framework_AssertionFailedError
Chris@0 129 *
Chris@0 130 * @see \Behat\Mink\Driver\DriverInterface::evaluateScript()
Chris@0 131 */
Chris@0 132 protected function assertJsCondition($condition, $timeout = 10000, $message = '') {
Chris@0 133 $message = $message ?: "Javascript condition met:\n" . $condition;
Chris@0 134 $result = $this->getSession()->getDriver()->wait($timeout, $condition);
Chris@0 135 $this->assertTrue($result, $message);
Chris@0 136 }
Chris@0 137
Chris@0 138 /**
Chris@0 139 * Creates a screenshot.
Chris@0 140 *
Chris@0 141 * @param string $filename
Chris@0 142 * The file name of the resulting screenshot. If using the default phantomjs
Chris@0 143 * driver then this should be a JPG filename.
Chris@0 144 * @param bool $set_background_color
Chris@0 145 * (optional) By default this method will set the background color to white.
Chris@0 146 * Set to FALSE to override this behaviour.
Chris@0 147 *
Chris@0 148 * @throws \Behat\Mink\Exception\UnsupportedDriverActionException
Chris@0 149 * When operation not supported by the driver.
Chris@0 150 * @throws \Behat\Mink\Exception\DriverException
Chris@0 151 * When the operation cannot be done.
Chris@0 152 */
Chris@0 153 protected function createScreenshot($filename, $set_background_color = TRUE) {
Chris@0 154 $session = $this->getSession();
Chris@0 155 if ($set_background_color) {
Chris@0 156 $session->executeScript("document.body.style.backgroundColor = 'white';");
Chris@0 157 }
Chris@0 158 $image = $session->getScreenshot();
Chris@0 159 file_put_contents($filename, $image);
Chris@0 160 }
Chris@0 161
Chris@0 162 /**
Chris@0 163 * {@inheritdoc}
Chris@0 164 */
Chris@0 165 public function assertSession($name = NULL) {
Chris@0 166 return new WebDriverWebAssert($this->getSession($name), $this->baseUrl);
Chris@0 167 }
Chris@0 168
Chris@0 169 /**
Chris@0 170 * Gets the current Drupal javascript settings and parses into an array.
Chris@0 171 *
Chris@0 172 * Unlike BrowserTestBase::getDrupalSettings(), this implementation reads the
Chris@0 173 * current values of drupalSettings, capturing all changes made via javascript
Chris@0 174 * after the page was loaded.
Chris@0 175 *
Chris@0 176 * @return array
Chris@0 177 * The Drupal javascript settings array.
Chris@0 178 *
Chris@0 179 * @see \Drupal\Tests\BrowserTestBase::getDrupalSettings()
Chris@0 180 */
Chris@0 181 protected function getDrupalSettings() {
Chris@0 182 $script = <<<EndOfScript
Chris@0 183 (function () {
Chris@0 184 if (typeof drupalSettings !== 'undefined') {
Chris@0 185 return drupalSettings;
Chris@0 186 }
Chris@0 187 })();
Chris@0 188 EndOfScript;
Chris@0 189
Chris@0 190 return $this->getSession()->evaluateScript($script) ?: [];
Chris@0 191 }
Chris@0 192
Chris@14 193 /**
Chris@14 194 * {@inheritdoc}
Chris@14 195 */
Chris@14 196 protected function getHtmlOutputHeaders() {
Chris@14 197 // The webdriver API does not support fetching headers.
Chris@14 198 return '';
Chris@14 199 }
Chris@14 200
Chris@0 201 }