3 namespace Drupal\FunctionalJavascriptTests;
5 use Drupal\Tests\BrowserTestBase;
6 use Zumba\GastonJS\Exception\DeadClient;
7 use Zumba\Mink\Driver\PhantomJSDriver;
10 * Runs a browser test using a driver that supports Javascript.
12 * Base class for testing browser interaction implemented in JavaScript.
14 abstract class JavascriptTestBase extends BrowserTestBase {
19 * To use a webdriver based approach, please use DrupalSelenium2Driver::class.
20 * We will switch the default later.
22 protected $minkDefaultDriverClass = PhantomJSDriver::class;
27 protected function initMink() {
28 if ($this->minkDefaultDriverClass === DrupalSelenium2Driver::class) {
29 $this->minkDefaultDriverArgs = ['chrome', NULL, 'http://localhost:4444/'];
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',
38 if (!file_exists($path)) {
44 return parent::initMink();
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&');
49 catch (\Exception $e) {
50 $this->markTestSkipped('An unexpected error occurred while starting Mink: ' . $e->getMessage());
57 protected function tearDown() {
59 // Wait for all requests to finish. It is possible that an AJAX request is
61 $result = $this->getSession()->wait(5000, '(typeof(jQuery)=="undefined" || (0 === jQuery.active && 0 === jQuery(\':animated\').length))');
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');
77 protected function getMinkDriverArgs() {
78 if ($this->minkDefaultDriverClass === DrupalSelenium2Driver::class) {
79 return getenv('MINK_DRIVER_ARGS_WEBDRIVER') ?: getenv('MINK_DRIVER_ARGS_PHANTOMJS') ?: parent::getMinkDriverArgs();
81 elseif ($this->minkDefaultDriverClass === PhantomJSDriver::class) {
82 return getenv('MINK_DRIVER_ARGS_PHANTOMJS') ?: parent::getMinkDriverArgs();
84 return parent::getMinkDriverArgs();
88 * Asserts that the element with the given CSS selector is visible.
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.
95 * @deprecated in Drupal 8.1.x, will be removed before Drupal 8.3.x. Use
96 * \Behat\Mink\Element\NodeElement::isVisible() instead.
98 protected function assertElementVisible($css_selector, $message = '') {
99 $this->assertTrue($this->getSession()->getDriver()->isVisible($this->cssSelectToXpath($css_selector)), $message);
103 * Asserts that the element with the given CSS selector is not visible.
105 * @param string $css_selector
106 * The CSS selector identifying the element to check.
107 * @param string $message
108 * Optional message to show alongside the assertion.
110 * @deprecated in Drupal 8.1.x, will be removed before Drupal 8.3.x. Use
111 * \Behat\Mink\Element\NodeElement::isVisible() instead.
113 protected function assertElementNotVisible($css_selector, $message = '') {
114 $this->assertFalse($this->getSession()->getDriver()->isVisible($this->cssSelectToXpath($css_selector)), $message);
118 * Waits for the given time or until the given JS condition becomes TRUE.
120 * @param string $condition
121 * JS condition to wait until it becomes TRUE.
122 * @param int $timeout
123 * (Optional) Timeout in milliseconds, defaults to 10000.
124 * @param string $message
125 * (optional) A message to display with the assertion. If left blank, a
126 * default message will be displayed.
128 * @throws \PHPUnit_Framework_AssertionFailedError
130 * @see \Behat\Mink\Driver\DriverInterface::evaluateScript()
132 protected function assertJsCondition($condition, $timeout = 10000, $message = '') {
133 $message = $message ?: "Javascript condition met:\n" . $condition;
134 $result = $this->getSession()->getDriver()->wait($timeout, $condition);
135 $this->assertTrue($result, $message);
139 * Creates a screenshot.
141 * @param string $filename
142 * The file name of the resulting screenshot. If using the default phantomjs
143 * driver then this should be a JPG filename.
144 * @param bool $set_background_color
145 * (optional) By default this method will set the background color to white.
146 * Set to FALSE to override this behaviour.
148 * @throws \Behat\Mink\Exception\UnsupportedDriverActionException
149 * When operation not supported by the driver.
150 * @throws \Behat\Mink\Exception\DriverException
151 * When the operation cannot be done.
153 protected function createScreenshot($filename, $set_background_color = TRUE) {
154 $session = $this->getSession();
155 if ($set_background_color) {
156 $session->executeScript("document.body.style.backgroundColor = 'white';");
158 $image = $session->getScreenshot();
159 file_put_contents($filename, $image);
165 public function assertSession($name = NULL) {
166 return new WebDriverWebAssert($this->getSession($name), $this->baseUrl);
170 * Gets the current Drupal javascript settings and parses into an array.
172 * Unlike BrowserTestBase::getDrupalSettings(), this implementation reads the
173 * current values of drupalSettings, capturing all changes made via javascript
174 * after the page was loaded.
177 * The Drupal javascript settings array.
179 * @see \Drupal\Tests\BrowserTestBase::getDrupalSettings()
181 protected function getDrupalSettings() {
182 $script = <<<EndOfScript
184 if (typeof drupalSettings !== 'undefined') {
185 return drupalSettings;
190 return $this->getSession()->evaluateScript($script) ?: [];
196 protected function getHtmlOutputHeaders() {
197 // The webdriver API does not support fetching headers.