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