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