comparison core/tests/Drupal/FunctionalJavascriptTests/JavascriptTestBase.php @ 0:c75dbcec494b

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