JavascriptTestTrait.php in Commerce Core 8.2
File
tests/src/Traits/JavascriptTestTrait.php
View source
<?php
namespace Drupal\Tests\commerce\Traits;
@trigger_error('The ' . __NAMESPACE__ . '\\JavascriptTestTrait is deprecated in Commerce 2.x and will be removed before Commerce 3.0.0. Instead, use ' . __NAMESPACE__ . '\\CommerceWebDriverTestBase. See https://www.drupal.org/project/commerce/issues/2998745', E_USER_DEPRECATED);
use Drupal\FunctionalJavascriptTests\JSWebAssert;
use Zumba\Mink\Driver\PhantomJSDriver;
trait JavascriptTestTrait {
protected function initMink() {
$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);
}
return parent::initMink();
}
protected function assertJsCondition($condition, $timeout = 1000, $message = '') {
$message = $message ?: "Javascript condition met:\n" . $condition;
$result = $this
->getSession()
->getDriver()
->wait($timeout, $condition);
$this
->assertNotEmpty($result, $message);
}
protected function waitForAjaxToFinish() {
$condition = "(0 === jQuery.active && 0 === jQuery(':animated').length)";
$this
->assertJsCondition($condition, 10000);
}
public function assertSession($name = NULL) {
return new JSWebAssert($this
->getSession($name), $this->baseUrl);
}
protected function createScreenshot($set_background_color = TRUE) {
$jpg_output_filename = $this->htmlOutputClassName . '-' . $this->htmlOutputCounter . '-' . $this->htmlOutputTestId . '.jpg';
$session = $this
->getSession();
if ($set_background_color) {
$session
->executeScript("document.body.style.backgroundColor = 'white';");
}
$image = $session
->getScreenshot();
file_put_contents($this->htmlOutputDirectory . '/' . $jpg_output_filename, $image);
$this->htmlOutputCounter++;
}
}