WebDriverTestBase.php in Drupal 8
File
core/tests/Drupal/FunctionalJavascriptTests/WebDriverTestBase.php
View source
<?php
namespace Drupal\FunctionalJavascriptTests;
use Behat\Mink\Exception\DriverException;
use Drupal\Tests\BrowserTestBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Zumba\GastonJS\Exception\DeadClient;
use Zumba\Mink\Driver\PhantomJSDriver;
abstract class WebDriverTestBase extends BrowserTestBase {
protected $disableCssAnimations = TRUE;
protected $minkDefaultDriverClass = DrupalSelenium2Driver::class;
protected function initMink() {
if ($this->minkDefaultDriverClass === DrupalSelenium2Driver::class) {
$this->minkDefaultDriverArgs = [
'chrome',
NULL,
'http://localhost:4444',
];
}
elseif ($this->minkDefaultDriverClass === PhantomJSDriver::class) {
$path = $this->tempFilesDirectory . DIRECTORY_SEPARATOR . 'browsertestbase-templatecache';
$this->minkDefaultDriverArgs = [
'http://127.0.0.1:8510',
$path,
];
if (!file_exists($path)) {
mkdir($path);
}
}
try {
return parent::initMink();
} catch (DeadClient $e) {
$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&');
} catch (DriverException $e) {
if ($this->minkDefaultDriverClass === DrupalSelenium2Driver::class) {
$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()}");
}
else {
throw $e;
}
} catch (\Exception $e) {
$this
->markTestSkipped('An unexpected error occurred while starting Mink: ' . $e
->getMessage());
}
}
protected function installModulesFromClassProperty(ContainerInterface $container) {
self::$modules = [
'js_deprecation_log_test',
];
if ($this->disableCssAnimations) {
self::$modules[] = 'css_disable_transitions_test';
}
parent::installModulesFromClassProperty($container);
}
protected function initFrontPage() {
parent::initFrontPage();
$this
->getSession()
->resizeWindow(1024, 768);
}
protected function tearDown() {
if ($this->mink) {
$result = $this
->getSession()
->wait(5000, '(typeof(jQuery)=="undefined" || (0 === jQuery.active && 0 === jQuery(\':animated\').length))');
if (!$result) {
throw new \RuntimeException('Unfinished AJAX requests while tearing down a test');
}
$warnings = $this
->getSession()
->evaluateScript("JSON.parse(sessionStorage.getItem('js_deprecation_log_test.warnings') || JSON.stringify([]))");
foreach ($warnings as $warning) {
if (strpos($warning, '[Deprecation]') === 0) {
@trigger_error('Javascript Deprecation:' . substr($warning, 13), E_USER_DEPRECATED);
}
}
}
parent::tearDown();
}
protected function getMinkDriverArgs() {
if ($this->minkDefaultDriverClass === DrupalSelenium2Driver::class) {
return getenv('MINK_DRIVER_ARGS_WEBDRIVER') ?: getenv('MINK_DRIVER_ARGS_PHANTOMJS') ?: parent::getMinkDriverArgs();
}
elseif ($this->minkDefaultDriverClass === PhantomJSDriver::class) {
return getenv('MINK_DRIVER_ARGS_PHANTOMJS') ?: parent::getMinkDriverArgs();
}
return parent::getMinkDriverArgs();
}
protected function assertElementVisible($css_selector, $message = '') {
$this
->assertTrue($this
->getSession()
->getDriver()
->isVisible($this
->cssSelectToXpath($css_selector)), $message);
@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);
}
protected function assertElementNotVisible($css_selector, $message = '') {
$this
->assertFalse($this
->getSession()
->getDriver()
->isVisible($this
->cssSelectToXpath($css_selector)), $message);
@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);
}
protected function assertJsCondition($condition, $timeout = 10000, $message = '') {
$message = $message ?: "Javascript condition met:\n" . $condition;
$result = $this
->getSession()
->getDriver()
->wait($timeout, $condition);
$this
->assertTrue($result, $message);
}
protected function createScreenshot($filename, $set_background_color = TRUE) {
$session = $this
->getSession();
if ($set_background_color) {
$session
->executeScript("document.body.style.backgroundColor = 'white';");
}
$image = $session
->getScreenshot();
file_put_contents($filename, $image);
}
public function assertSession($name = NULL) {
return new WebDriverWebAssert($this
->getSession($name), $this->baseUrl);
}
protected function getDrupalSettings() {
$script = <<<EndOfScript
(function () {
if (typeof drupalSettings !== 'undefined') {
return drupalSettings;
}
})();
EndOfScript;
return $this
->getSession()
->evaluateScript($script) ?: [];
}
protected function getHtmlOutputHeaders() {
return '';
}
}