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@0
|
18 *
|
Chris@0
|
19 * To use a webdriver based approach, please use DrupalSelenium2Driver::class.
|
Chris@0
|
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@0
|
28 if ($this->minkDefaultDriverClass === DrupalSelenium2Driver::class) {
|
Chris@0
|
29 $this->minkDefaultDriverArgs = ['chrome', NULL, 'http://localhost:4444/'];
|
Chris@0
|
30 }
|
Chris@0
|
31 elseif ($this->minkDefaultDriverClass === PhantomJSDriver::class) {
|
Chris@0
|
32 // Set up the template cache used by the PhantomJS mink driver.
|
Chris@0
|
33 $path = $this->tempFilesDirectory . DIRECTORY_SEPARATOR . 'browsertestbase-templatecache';
|
Chris@0
|
34 $this->minkDefaultDriverArgs = [
|
Chris@0
|
35 'http://127.0.0.1:8510',
|
Chris@0
|
36 $path,
|
Chris@0
|
37 ];
|
Chris@0
|
38 if (!file_exists($path)) {
|
Chris@0
|
39 mkdir($path);
|
Chris@0
|
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@0
|
75 * {@inheritdoc}
|
Chris@0
|
76 */
|
Chris@0
|
77 protected function getMinkDriverArgs() {
|
Chris@0
|
78 if ($this->minkDefaultDriverClass === DrupalSelenium2Driver::class) {
|
Chris@0
|
79 return getenv('MINK_DRIVER_ARGS_WEBDRIVER') ?: getenv('MINK_DRIVER_ARGS_PHANTOMJS') ?: parent::getMinkDriverArgs();
|
Chris@0
|
80 }
|
Chris@0
|
81 elseif ($this->minkDefaultDriverClass === PhantomJSDriver::class) {
|
Chris@0
|
82 return getenv('MINK_DRIVER_ARGS_PHANTOMJS') ?: parent::getMinkDriverArgs();
|
Chris@0
|
83 }
|
Chris@0
|
84 return parent::getMinkDriverArgs();
|
Chris@0
|
85 }
|
Chris@0
|
86
|
Chris@0
|
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.0, will be removed before Drupal 9.0.0. 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 @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@0
|
101 }
|
Chris@0
|
102
|
Chris@0
|
103 /**
|
Chris@0
|
104 * Asserts that the element with the given CSS selector is not visible.
|
Chris@0
|
105 *
|
Chris@0
|
106 * @param string $css_selector
|
Chris@0
|
107 * The CSS selector identifying the element to check.
|
Chris@0
|
108 * @param string $message
|
Chris@0
|
109 * Optional message to show alongside the assertion.
|
Chris@0
|
110 *
|
Chris@0
|
111 * @deprecated in Drupal 8.1.0, will be removed before Drupal 9.0.0. Use
|
Chris@0
|
112 * \Behat\Mink\Element\NodeElement::isVisible() instead.
|
Chris@0
|
113 */
|
Chris@0
|
114 protected function assertElementNotVisible($css_selector, $message = '') {
|
Chris@0
|
115 $this->assertFalse($this->getSession()->getDriver()->isVisible($this->cssSelectToXpath($css_selector)), $message);
|
Chris@0
|
116 @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@0
|
117 }
|
Chris@0
|
118
|
Chris@0
|
119 /**
|
Chris@0
|
120 * Waits for the given time or until the given JS condition becomes TRUE.
|
Chris@0
|
121 *
|
Chris@0
|
122 * @param string $condition
|
Chris@0
|
123 * JS condition to wait until it becomes TRUE.
|
Chris@0
|
124 * @param int $timeout
|
Chris@0
|
125 * (Optional) Timeout in milliseconds, defaults to 10000.
|
Chris@0
|
126 * @param string $message
|
Chris@0
|
127 * (optional) A message to display with the assertion. If left blank, a
|
Chris@0
|
128 * default message will be displayed.
|
Chris@0
|
129 *
|
Chris@0
|
130 * @throws \PHPUnit_Framework_AssertionFailedError
|
Chris@0
|
131 *
|
Chris@0
|
132 * @see \Behat\Mink\Driver\DriverInterface::evaluateScript()
|
Chris@0
|
133 */
|
Chris@0
|
134 protected function assertJsCondition($condition, $timeout = 10000, $message = '') {
|
Chris@0
|
135 $message = $message ?: "Javascript condition met:\n" . $condition;
|
Chris@0
|
136 $result = $this->getSession()->getDriver()->wait($timeout, $condition);
|
Chris@0
|
137 $this->assertTrue($result, $message);
|
Chris@0
|
138 }
|
Chris@0
|
139
|
Chris@0
|
140 /**
|
Chris@0
|
141 * Creates a screenshot.
|
Chris@0
|
142 *
|
Chris@0
|
143 * @param string $filename
|
Chris@0
|
144 * The file name of the resulting screenshot. If using the default phantomjs
|
Chris@0
|
145 * driver then this should be a JPG filename.
|
Chris@0
|
146 * @param bool $set_background_color
|
Chris@0
|
147 * (optional) By default this method will set the background color to white.
|
Chris@0
|
148 * Set to FALSE to override this behaviour.
|
Chris@0
|
149 *
|
Chris@0
|
150 * @throws \Behat\Mink\Exception\UnsupportedDriverActionException
|
Chris@0
|
151 * When operation not supported by the driver.
|
Chris@0
|
152 * @throws \Behat\Mink\Exception\DriverException
|
Chris@0
|
153 * When the operation cannot be done.
|
Chris@0
|
154 */
|
Chris@0
|
155 protected function createScreenshot($filename, $set_background_color = TRUE) {
|
Chris@0
|
156 $session = $this->getSession();
|
Chris@0
|
157 if ($set_background_color) {
|
Chris@0
|
158 $session->executeScript("document.body.style.backgroundColor = 'white';");
|
Chris@0
|
159 }
|
Chris@0
|
160 $image = $session->getScreenshot();
|
Chris@0
|
161 file_put_contents($filename, $image);
|
Chris@0
|
162 }
|
Chris@0
|
163
|
Chris@0
|
164 /**
|
Chris@0
|
165 * {@inheritdoc}
|
Chris@0
|
166 */
|
Chris@0
|
167 public function assertSession($name = NULL) {
|
Chris@0
|
168 return new WebDriverWebAssert($this->getSession($name), $this->baseUrl);
|
Chris@0
|
169 }
|
Chris@0
|
170
|
Chris@0
|
171 /**
|
Chris@0
|
172 * Gets the current Drupal javascript settings and parses into an array.
|
Chris@0
|
173 *
|
Chris@0
|
174 * Unlike BrowserTestBase::getDrupalSettings(), this implementation reads the
|
Chris@0
|
175 * current values of drupalSettings, capturing all changes made via javascript
|
Chris@0
|
176 * after the page was loaded.
|
Chris@0
|
177 *
|
Chris@0
|
178 * @return array
|
Chris@0
|
179 * The Drupal javascript settings array.
|
Chris@0
|
180 *
|
Chris@0
|
181 * @see \Drupal\Tests\BrowserTestBase::getDrupalSettings()
|
Chris@0
|
182 */
|
Chris@0
|
183 protected function getDrupalSettings() {
|
Chris@0
|
184 $script = <<<EndOfScript
|
Chris@0
|
185 (function () {
|
Chris@0
|
186 if (typeof drupalSettings !== 'undefined') {
|
Chris@0
|
187 return drupalSettings;
|
Chris@0
|
188 }
|
Chris@0
|
189 })();
|
Chris@0
|
190 EndOfScript;
|
Chris@0
|
191
|
Chris@0
|
192 return $this->getSession()->evaluateScript($script) ?: [];
|
Chris@0
|
193 }
|
Chris@0
|
194
|
Chris@0
|
195 /**
|
Chris@0
|
196 * {@inheritdoc}
|
Chris@0
|
197 */
|
Chris@0
|
198 protected function getHtmlOutputHeaders() {
|
Chris@0
|
199 // The webdriver API does not support fetching headers.
|
Chris@0
|
200 return '';
|
Chris@0
|
201 }
|
Chris@0
|
202
|
Chris@0
|
203 }
|