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