View source
<?php
namespace Drupal\FunctionalJavascriptTests;
use Behat\Mink\Driver\Selenium2Driver;
use Behat\Mink\Exception\DriverException;
use WebDriver\Exception;
use WebDriver\Exception\UnknownError;
use WebDriver\ServiceFactory;
class DrupalSelenium2Driver extends Selenium2Driver {
public function __construct($browserName = 'firefox', $desiredCapabilities = NULL, $wdHost = 'http://localhost:4444/wd/hub') {
parent::__construct($browserName, $desiredCapabilities, $wdHost);
ServiceFactory::getInstance()
->setServiceClass('service.curl', WebDriverCurlService::class);
}
public function setCookie($name, $value = NULL) {
if ($value === NULL) {
$this
->getWebDriverSession()
->deleteCookie($name);
return;
}
$cookieArray = [
'name' => $name,
'value' => urlencode($value),
'secure' => FALSE,
'domain' => parse_url($this
->getWebDriverSession()
->url(), PHP_URL_HOST),
'expires' => time() + 80000,
];
$this
->getWebDriverSession()
->setCookie($cookieArray);
}
public function attachFile($xpath, $path) {
$element = $this
->getWebDriverSession()
->element('xpath', $xpath);
if ('input' !== strtolower($element
->name()) || 'file' !== strtolower($element
->attribute('type'))) {
$message = 'Impossible to %s the element with XPath "%s" as it is not a %s input';
throw new DriverException(sprintf($message, 'attach a file on', $xpath, 'file'));
}
try {
$remotePath = $this
->uploadFileAndGetRemoteFilePath($path);
} catch (\Exception $e) {
$remotePath = $path;
}
$element
->postValue([
'value' => [
$remotePath,
],
]);
}
public function uploadFileAndGetRemoteFilePath($path) {
if (!is_file($path)) {
throw new DriverException('File does not exist locally and cannot be uploaded to the remote instance.');
}
if (!class_exists('ZipArchive')) {
throw new DriverException('Could not compress file, PHP is compiled without zip support.');
}
$tempFilename = tempnam('', 'WebDriverZip');
$archive = new \ZipArchive();
$result = $archive
->open($tempFilename, \ZipArchive::OVERWRITE);
if (!$result) {
throw new DriverException('Zip archive could not be created. Error ' . $result);
}
$result = $archive
->addFile($path, basename($path));
if (!$result) {
throw new DriverException('File could not be added to zip archive.');
}
$result = $archive
->close();
if (!$result) {
throw new DriverException('Zip archive could not be closed.');
}
try {
$remotePath = $this
->getWebDriverSession()
->file([
'file' => base64_encode(file_get_contents($tempFilename)),
]);
if (empty($remotePath)) {
throw new UnknownError();
}
} catch (\Exception $e) {
throw $e;
} finally {
unlink($tempFilename);
}
return $remotePath;
}
public function click($xpath) {
$not_clickable_exception = NULL;
$result = $this
->waitFor(10, function () use (&$not_clickable_exception, $xpath) {
try {
parent::click($xpath);
return TRUE;
} catch (Exception $exception) {
if (!JSWebAssert::isExceptionNotClickable($exception)) {
throw $exception;
}
$not_clickable_exception = $exception;
return NULL;
}
});
if ($result !== TRUE) {
throw $not_clickable_exception;
}
}
public function setValue($xpath, $value) {
$not_clickable_exception = NULL;
$result = $this
->waitFor(10, function () use (&$not_clickable_exception, $xpath, $value) {
try {
parent::setValue($xpath, $value);
return TRUE;
} catch (Exception $exception) {
if (!JSWebAssert::isExceptionNotClickable($exception) && !str_contains($exception
->getMessage(), 'invalid element state')) {
throw $exception;
}
$not_clickable_exception = $exception;
return NULL;
}
});
if ($result !== TRUE) {
throw $not_clickable_exception;
}
}
private function waitFor($timeout, callable $callback) {
$start = microtime(TRUE);
$end = $start + $timeout;
do {
$result = call_user_func($callback, $this);
if ($result) {
break;
}
usleep(10000);
} while (microtime(TRUE) < $end);
return $result;
}
}