Chris@4: minkDefaultDriverClass === DrupalSelenium2Driver::class) { Chris@4: $this->minkDefaultDriverArgs = ['chrome', NULL, 'http://localhost:4444']; Chris@4: } Chris@4: elseif ($this->minkDefaultDriverClass === PhantomJSDriver::class) { Chris@4: // Set up the template cache used by the PhantomJS mink driver. Chris@4: $path = $this->tempFilesDirectory . DIRECTORY_SEPARATOR . 'browsertestbase-templatecache'; Chris@4: $this->minkDefaultDriverArgs = [ Chris@4: 'http://127.0.0.1:8510', Chris@4: $path, Chris@4: ]; Chris@4: if (!file_exists($path)) { Chris@4: mkdir($path); Chris@4: } Chris@4: } Chris@4: Chris@4: try { Chris@4: return parent::initMink(); Chris@4: } Chris@4: catch (DeadClient $e) { Chris@4: $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@4: } Chris@4: catch (DriverException $e) { Chris@4: if ($this->minkDefaultDriverClass === DrupalSelenium2Driver::class) { Chris@4: $this->markTestSkipped("The test wasn't able to connect to your webdriver instance. For more information read core/tests/README.md.\n\nThe original message while starting Mink: {$e->getMessage()}"); Chris@4: } Chris@4: else { Chris@4: throw $e; Chris@4: } Chris@4: } Chris@4: catch (\Exception $e) { Chris@4: $this->markTestSkipped('An unexpected error occurred while starting Mink: ' . $e->getMessage()); Chris@4: } Chris@4: } Chris@4: Chris@4: /** Chris@4: * {@inheritdoc} Chris@4: */ Chris@4: protected function tearDown() { Chris@4: if ($this->mink) { Chris@4: // Wait for all requests to finish. It is possible that an AJAX request is Chris@4: // still on-going. Chris@4: $result = $this->getSession()->wait(5000, '(typeof(jQuery)=="undefined" || (0 === jQuery.active && 0 === jQuery(\':animated\').length))'); Chris@4: if (!$result) { Chris@4: // If the wait is unsuccessful, there may still be an AJAX request in Chris@4: // progress. If we tear down now, then this AJAX request may fail with Chris@4: // missing database tables, because tear down will have removed them. Chris@4: // Rather than allow it to fail, throw an explicit exception now Chris@4: // explaining what the problem is. Chris@4: throw new \RuntimeException('Unfinished AJAX requests while tearing down a test'); Chris@4: } Chris@4: } Chris@4: parent::tearDown(); Chris@4: } Chris@4: Chris@4: /** Chris@4: * {@inheritdoc} Chris@4: */ Chris@4: protected function getMinkDriverArgs() { Chris@4: if ($this->minkDefaultDriverClass === DrupalSelenium2Driver::class) { Chris@4: return getenv('MINK_DRIVER_ARGS_WEBDRIVER') ?: getenv('MINK_DRIVER_ARGS_PHANTOMJS') ?: parent::getMinkDriverArgs(); Chris@4: } Chris@4: elseif ($this->minkDefaultDriverClass === PhantomJSDriver::class) { Chris@4: return getenv('MINK_DRIVER_ARGS_PHANTOMJS') ?: parent::getMinkDriverArgs(); Chris@4: } Chris@4: return parent::getMinkDriverArgs(); Chris@4: } Chris@4: Chris@4: /** Chris@4: * Asserts that the element with the given CSS selector is visible. Chris@4: * Chris@4: * @param string $css_selector Chris@4: * The CSS selector identifying the element to check. Chris@4: * @param string $message Chris@4: * Optional message to show alongside the assertion. Chris@4: * Chris@4: * @deprecated in Drupal 8.1.0, will be removed before Drupal 9.0.0. Use Chris@4: * \Behat\Mink\Element\NodeElement::isVisible() instead. Chris@4: */ Chris@4: protected function assertElementVisible($css_selector, $message = '') { Chris@4: $this->assertTrue($this->getSession()->getDriver()->isVisible($this->cssSelectToXpath($css_selector)), $message); Chris@4: @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); Chris@4: } Chris@4: Chris@4: /** Chris@4: * Asserts that the element with the given CSS selector is not visible. Chris@4: * Chris@4: * @param string $css_selector Chris@4: * The CSS selector identifying the element to check. Chris@4: * @param string $message Chris@4: * Optional message to show alongside the assertion. Chris@4: * Chris@4: * @deprecated in Drupal 8.1.0, will be removed before Drupal 9.0.0. Use Chris@4: * \Behat\Mink\Element\NodeElement::isVisible() instead. Chris@4: */ Chris@4: protected function assertElementNotVisible($css_selector, $message = '') { Chris@4: $this->assertFalse($this->getSession()->getDriver()->isVisible($this->cssSelectToXpath($css_selector)), $message); Chris@4: @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); Chris@4: } Chris@4: Chris@4: /** Chris@4: * Waits for the given time or until the given JS condition becomes TRUE. Chris@4: * Chris@4: * @param string $condition Chris@4: * JS condition to wait until it becomes TRUE. Chris@4: * @param int $timeout Chris@4: * (Optional) Timeout in milliseconds, defaults to 10000. Chris@4: * @param string $message Chris@4: * (optional) A message to display with the assertion. If left blank, a Chris@4: * default message will be displayed. Chris@4: * Chris@4: * @throws \PHPUnit_Framework_AssertionFailedError Chris@4: * Chris@4: * @see \Behat\Mink\Driver\DriverInterface::evaluateScript() Chris@4: */ Chris@4: protected function assertJsCondition($condition, $timeout = 10000, $message = '') { Chris@4: $message = $message ?: "Javascript condition met:\n" . $condition; Chris@4: $result = $this->getSession()->getDriver()->wait($timeout, $condition); Chris@4: $this->assertTrue($result, $message); Chris@4: } Chris@4: Chris@4: /** Chris@4: * Creates a screenshot. Chris@4: * Chris@4: * @param string $filename Chris@4: * The file name of the resulting screenshot. If using the default phantomjs Chris@4: * driver then this should be a JPG filename. Chris@4: * @param bool $set_background_color Chris@4: * (optional) By default this method will set the background color to white. Chris@4: * Set to FALSE to override this behaviour. Chris@4: * Chris@4: * @throws \Behat\Mink\Exception\UnsupportedDriverActionException Chris@4: * When operation not supported by the driver. Chris@4: * @throws \Behat\Mink\Exception\DriverException Chris@4: * When the operation cannot be done. Chris@4: */ Chris@4: protected function createScreenshot($filename, $set_background_color = TRUE) { Chris@4: $session = $this->getSession(); Chris@4: if ($set_background_color) { Chris@4: $session->executeScript("document.body.style.backgroundColor = 'white';"); Chris@4: } Chris@4: $image = $session->getScreenshot(); Chris@4: file_put_contents($filename, $image); Chris@4: } Chris@4: Chris@4: /** Chris@4: * {@inheritdoc} Chris@4: */ Chris@4: public function assertSession($name = NULL) { Chris@4: return new WebDriverWebAssert($this->getSession($name), $this->baseUrl); Chris@4: } Chris@4: Chris@4: /** Chris@4: * Gets the current Drupal javascript settings and parses into an array. Chris@4: * Chris@4: * Unlike BrowserTestBase::getDrupalSettings(), this implementation reads the Chris@4: * current values of drupalSettings, capturing all changes made via javascript Chris@4: * after the page was loaded. Chris@4: * Chris@4: * @return array Chris@4: * The Drupal javascript settings array. Chris@4: * Chris@4: * @see \Drupal\Tests\BrowserTestBase::getDrupalSettings() Chris@4: */ Chris@4: protected function getDrupalSettings() { Chris@4: $script = <<getSession()->evaluateScript($script) ?: []; Chris@4: } Chris@4: Chris@4: /** Chris@4: * {@inheritdoc} Chris@4: */ Chris@4: protected function getHtmlOutputHeaders() { Chris@4: // The webdriver API does not support fetching headers. Chris@4: return ''; Chris@4: } Chris@4: Chris@4: }