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 PhantomJS.
12 * Base class for testing browser interaction implemented in JavaScript.
14 abstract class JavascriptTestBase extends BrowserTestBase {
19 protected $minkDefaultDriverClass = PhantomJSDriver::class;
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',
31 if (!file_exists($path)) {
36 return parent::initMink();
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&');
41 catch (\Exception $e) {
42 $this->markTestSkipped('An unexpected error occurred while starting Mink: ' . $e->getMessage());
49 protected function tearDown() {
51 // Wait for all requests to finish. It is possible that an AJAX request is
53 $result = $this->getSession()->wait(5000, '(typeof(jQuery)=="undefined" || (0 === jQuery.active && 0 === jQuery(\':animated\').length))');
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');
67 * Asserts that the element with the given CSS selector is visible.
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.
74 * @deprecated in Drupal 8.1.x, will be removed before Drupal 8.3.x. Use
75 * \Behat\Mink\Element\NodeElement::isVisible() instead.
77 protected function assertElementVisible($css_selector, $message = '') {
78 $this->assertTrue($this->getSession()->getDriver()->isVisible($this->cssSelectToXpath($css_selector)), $message);
82 * Asserts that the element with the given CSS selector is not visible.
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.
89 * @deprecated in Drupal 8.1.x, will be removed before Drupal 8.3.x. Use
90 * \Behat\Mink\Element\NodeElement::isVisible() instead.
92 protected function assertElementNotVisible($css_selector, $message = '') {
93 $this->assertFalse($this->getSession()->getDriver()->isVisible($this->cssSelectToXpath($css_selector)), $message);
97 * Waits for the given time or until the given JS condition becomes TRUE.
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.
107 * @throws \PHPUnit_Framework_AssertionFailedError
109 * @see \Behat\Mink\Driver\DriverInterface::evaluateScript()
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);
118 * Creates a screenshot.
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.
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.
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';");
137 $image = $session->getScreenshot();
138 file_put_contents($filename, $image);
144 public function assertSession($name = NULL) {
145 return new JSWebAssert($this->getSession($name), $this->baseUrl);
149 * Gets the current Drupal javascript settings and parses into an array.
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.
156 * The Drupal javascript settings array.
158 * @see \Drupal\Tests\BrowserTestBase::getDrupalSettings()
160 protected function getDrupalSettings() {
161 $script = <<<EndOfScript
163 if (typeof drupalSettings !== 'undefined') {
164 return drupalSettings;
169 return $this->getSession()->evaluateScript($script) ?: [];