Mercurial > hg > isophonics-drupal-site
comparison core/tests/Drupal/FunctionalJavascriptTests/JavascriptTestBase.php @ 0:4c8ae668cc8c
Initial import (non-working)
author | Chris Cannam |
---|---|
date | Wed, 29 Nov 2017 16:09:58 +0000 |
parents | |
children | 1fec387a4317 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4c8ae668cc8c |
---|---|
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 protected $minkDefaultDriverClass = PhantomJSDriver::class; | |
20 | |
21 /** | |
22 * {@inheritdoc} | |
23 */ | |
24 protected function initMink() { | |
25 // Set up the template cache used by the PhantomJS mink driver. | |
26 $path = $this->tempFilesDirectory . DIRECTORY_SEPARATOR . 'browsertestbase-templatecache'; | |
27 $this->minkDefaultDriverArgs = [ | |
28 'http://127.0.0.1:8510', | |
29 $path, | |
30 ]; | |
31 if (!file_exists($path)) { | |
32 mkdir($path); | |
33 } | |
34 | |
35 try { | |
36 return parent::initMink(); | |
37 } | |
38 catch (DeadClient $e) { | |
39 $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&'); | |
40 } | |
41 catch (\Exception $e) { | |
42 $this->markTestSkipped('An unexpected error occurred while starting Mink: ' . $e->getMessage()); | |
43 } | |
44 } | |
45 | |
46 /** | |
47 * {@inheritdoc} | |
48 */ | |
49 protected function tearDown() { | |
50 if ($this->mink) { | |
51 // Wait for all requests to finish. It is possible that an AJAX request is | |
52 // still on-going. | |
53 $result = $this->getSession()->wait(5000, '(typeof(jQuery)=="undefined" || (0 === jQuery.active && 0 === jQuery(\':animated\').length))'); | |
54 if (!$result) { | |
55 // If the wait is unsuccessful, there may still be an AJAX request in | |
56 // progress. If we tear down now, then this AJAX request may fail with | |
57 // missing database tables, because tear down will have removed them. | |
58 // Rather than allow it to fail, throw an explicit exception now | |
59 // explaining what the problem is. | |
60 throw new \RuntimeException('Unfinished AJAX requests while tearing down a test'); | |
61 } | |
62 } | |
63 parent::tearDown(); | |
64 } | |
65 | |
66 /** | |
67 * Asserts that the element with the given CSS selector is visible. | |
68 * | |
69 * @param string $css_selector | |
70 * The CSS selector identifying the element to check. | |
71 * @param string $message | |
72 * Optional message to show alongside the assertion. | |
73 * | |
74 * @deprecated in Drupal 8.1.x, will be removed before Drupal 8.3.x. Use | |
75 * \Behat\Mink\Element\NodeElement::isVisible() instead. | |
76 */ | |
77 protected function assertElementVisible($css_selector, $message = '') { | |
78 $this->assertTrue($this->getSession()->getDriver()->isVisible($this->cssSelectToXpath($css_selector)), $message); | |
79 } | |
80 | |
81 /** | |
82 * Asserts that the element with the given CSS selector is not visible. | |
83 * | |
84 * @param string $css_selector | |
85 * The CSS selector identifying the element to check. | |
86 * @param string $message | |
87 * Optional message to show alongside the assertion. | |
88 * | |
89 * @deprecated in Drupal 8.1.x, will be removed before Drupal 8.3.x. Use | |
90 * \Behat\Mink\Element\NodeElement::isVisible() instead. | |
91 */ | |
92 protected function assertElementNotVisible($css_selector, $message = '') { | |
93 $this->assertFalse($this->getSession()->getDriver()->isVisible($this->cssSelectToXpath($css_selector)), $message); | |
94 } | |
95 | |
96 /** | |
97 * Waits for the given time or until the given JS condition becomes TRUE. | |
98 * | |
99 * @param string $condition | |
100 * JS condition to wait until it becomes TRUE. | |
101 * @param int $timeout | |
102 * (Optional) Timeout in milliseconds, defaults to 10000. | |
103 * @param string $message | |
104 * (optional) A message to display with the assertion. If left blank, a | |
105 * default message will be displayed. | |
106 * | |
107 * @throws \PHPUnit_Framework_AssertionFailedError | |
108 * | |
109 * @see \Behat\Mink\Driver\DriverInterface::evaluateScript() | |
110 */ | |
111 protected function assertJsCondition($condition, $timeout = 10000, $message = '') { | |
112 $message = $message ?: "Javascript condition met:\n" . $condition; | |
113 $result = $this->getSession()->getDriver()->wait($timeout, $condition); | |
114 $this->assertTrue($result, $message); | |
115 } | |
116 | |
117 /** | |
118 * Creates a screenshot. | |
119 * | |
120 * @param string $filename | |
121 * The file name of the resulting screenshot. If using the default phantomjs | |
122 * driver then this should be a JPG filename. | |
123 * @param bool $set_background_color | |
124 * (optional) By default this method will set the background color to white. | |
125 * Set to FALSE to override this behaviour. | |
126 * | |
127 * @throws \Behat\Mink\Exception\UnsupportedDriverActionException | |
128 * When operation not supported by the driver. | |
129 * @throws \Behat\Mink\Exception\DriverException | |
130 * When the operation cannot be done. | |
131 */ | |
132 protected function createScreenshot($filename, $set_background_color = TRUE) { | |
133 $session = $this->getSession(); | |
134 if ($set_background_color) { | |
135 $session->executeScript("document.body.style.backgroundColor = 'white';"); | |
136 } | |
137 $image = $session->getScreenshot(); | |
138 file_put_contents($filename, $image); | |
139 } | |
140 | |
141 /** | |
142 * {@inheritdoc} | |
143 */ | |
144 public function assertSession($name = NULL) { | |
145 return new WebDriverWebAssert($this->getSession($name), $this->baseUrl); | |
146 } | |
147 | |
148 /** | |
149 * Gets the current Drupal javascript settings and parses into an array. | |
150 * | |
151 * Unlike BrowserTestBase::getDrupalSettings(), this implementation reads the | |
152 * current values of drupalSettings, capturing all changes made via javascript | |
153 * after the page was loaded. | |
154 * | |
155 * @return array | |
156 * The Drupal javascript settings array. | |
157 * | |
158 * @see \Drupal\Tests\BrowserTestBase::getDrupalSettings() | |
159 */ | |
160 protected function getDrupalSettings() { | |
161 $script = <<<EndOfScript | |
162 (function () { | |
163 if (typeof drupalSettings !== 'undefined') { | |
164 return drupalSettings; | |
165 } | |
166 })(); | |
167 EndOfScript; | |
168 | |
169 return $this->getSession()->evaluateScript($script) ?: []; | |
170 } | |
171 | |
172 } |