WebDriverTestBase.php in Drupal 9
File
core/tests/Drupal/FunctionalJavascriptTests/WebDriverTestBase.php
View source
<?php
namespace Drupal\FunctionalJavascriptTests;
use Behat\Mink\Exception\DriverException;
use Drupal\Tests\BrowserTestBase;
use PHPUnit\Runner\BaseTestRunner;
use Symfony\Component\DependencyInjection\ContainerInterface;
abstract class WebDriverTestBase extends BrowserTestBase {
protected $failOnJavascriptConsoleErrors = TRUE;
protected $disableCssAnimations = TRUE;
protected $minkDefaultDriverClass = DrupalSelenium2Driver::class;
protected function initMink() {
if (!is_a($this->minkDefaultDriverClass, DrupalSelenium2Driver::class, TRUE)) {
throw new \UnexpectedValueException(sprintf("%s has to be an instance of %s", $this->minkDefaultDriverClass, DrupalSelenium2Driver::class));
}
$this->minkDefaultDriverArgs = [
'chrome',
NULL,
'http://localhost:4444',
];
try {
return parent::initMink();
} 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_testing_log_test',
'jquery_keyevent_polyfill_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) {
$status = $this
->getStatus();
if ($status === BaseTestRunner::STATUS_ERROR || $status === BaseTestRunner::STATUS_WARNING || $status === BaseTestRunner::STATUS_FAILURE) {
@$this
->htmlOutput();
}
$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_testing_log_test.warnings') || JSON.stringify([]))");
foreach ($warnings as $warning) {
if (strpos($warning, '[Deprecation]') === 0) {
@trigger_error('Javascript Deprecation:' . substr($warning, 13), E_USER_DEPRECATED);
}
}
if ($this->failOnJavascriptConsoleErrors) {
$errors = $this
->getSession()
->evaluateScript("JSON.parse(sessionStorage.getItem('js_testing_log_test.errors') || JSON.stringify([]))");
if (!empty($errors)) {
$all_errors = implode("\n", $errors);
@trigger_error("Not failing JavaScript test for JavaScript errors is deprecated in drupal:9.3.0 and is removed from drupal:10.0.0. This test had the following JavaScript errors: {$all_errors}. See https://www.drupal.org/node/3221100", E_USER_DEPRECATED);
}
}
}
parent::tearDown();
}
protected function getMinkDriverArgs() {
if ($this->minkDefaultDriverClass === DrupalSelenium2Driver::class) {
return getenv('MINK_DRIVER_ARGS_WEBDRIVER') ?: parent::getMinkDriverArgs();
}
return parent::getMinkDriverArgs();
}
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 '';
}
}