DrupalSelenium2Driver.php in Drupal 8
File
core/tests/Drupal/FunctionalJavascriptTests/DrupalSelenium2Driver.php
View source
<?php
namespace Drupal\FunctionalJavascriptTests;
use Behat\Mink\Driver\Selenium2Driver;
use Behat\Mink\Exception\DriverException;
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 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::CREATE);
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;
}
}