annotate core/tests/Drupal/FunctionalJavascriptTests/WebDriverTestBase.php @ 4:a9cd425dd02b

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