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