annotate core/tests/Drupal/FunctionalJavascriptTests/WebDriverTestBase.php @ 5:12f9dff5fda9 tip

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