annotate core/tests/Drupal/FunctionalJavascriptTests/DrupalSelenium2Driver.php @ 5:12f9dff5fda9 tip

Update to Drupal core 8.7.1
author Chris Cannam
date Thu, 09 May 2019 15:34:47 +0100
parents a9cd425dd02b
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\FunctionalJavascriptTests;
Chris@0 4
Chris@0 5 use Behat\Mink\Driver\Selenium2Driver;
Chris@5 6 use Behat\Mink\Exception\DriverException;
Chris@5 7 use WebDriver\Exception\UnknownError;
Chris@4 8 use WebDriver\ServiceFactory;
Chris@0 9
Chris@0 10 /**
Chris@0 11 * Provides a driver for Selenium testing.
Chris@0 12 */
Chris@0 13 class DrupalSelenium2Driver extends Selenium2Driver {
Chris@0 14
Chris@0 15 /**
Chris@0 16 * {@inheritdoc}
Chris@0 17 */
Chris@4 18 public function __construct($browserName = 'firefox', $desiredCapabilities = NULL, $wdHost = 'http://localhost:4444/wd/hub') {
Chris@4 19 parent::__construct($browserName, $desiredCapabilities, $wdHost);
Chris@4 20 ServiceFactory::getInstance()->setServiceClass('service.curl', WebDriverCurlService::class);
Chris@4 21 }
Chris@4 22
Chris@4 23 /**
Chris@4 24 * {@inheritdoc}
Chris@4 25 */
Chris@0 26 public function setCookie($name, $value = NULL) {
Chris@0 27 if ($value === NULL) {
Chris@0 28 $this->getWebDriverSession()->deleteCookie($name);
Chris@0 29 return;
Chris@0 30 }
Chris@0 31
Chris@0 32 $cookieArray = [
Chris@0 33 'name' => $name,
Chris@0 34 'value' => urlencode($value),
Chris@0 35 'secure' => FALSE,
Chris@0 36 // Unlike \Behat\Mink\Driver\Selenium2Driver::setCookie we set a domain
Chris@0 37 // and an expire date, as otherwise cookies leak from one test site into
Chris@0 38 // another.
Chris@0 39 'domain' => parse_url($this->getWebDriverSession()->url(), PHP_URL_HOST),
Chris@0 40 'expires' => time() + 80000,
Chris@0 41 ];
Chris@0 42
Chris@0 43 $this->getWebDriverSession()->setCookie($cookieArray);
Chris@0 44 }
Chris@0 45
Chris@5 46 /**
Chris@5 47 * Uploads a file to the Selenium instance and returns the remote path.
Chris@5 48 *
Chris@5 49 * \Behat\Mink\Driver\Selenium2Driver::uploadFile() is a private method so
Chris@5 50 * that can't be used inside a test, but we need the remote path that is
Chris@5 51 * generated when uploading to make sure the file reference exists on the
Chris@5 52 * container running selenium.
Chris@5 53 *
Chris@5 54 * @param string $path
Chris@5 55 * The path to the file to upload.
Chris@5 56 *
Chris@5 57 * @return string
Chris@5 58 * The remote path.
Chris@5 59 *
Chris@5 60 * @throws \Behat\Mink\Exception\DriverException
Chris@5 61 * When PHP is compiled without zip support, or the file doesn't exist.
Chris@5 62 * @throws \WebDriver\Exception\UnknownError
Chris@5 63 * When an unknown error occurred during file upload.
Chris@5 64 * @throws \Exception
Chris@5 65 * When a known error occurred during file upload.
Chris@5 66 */
Chris@5 67 public function uploadFileAndGetRemoteFilePath($path) {
Chris@5 68 if (!is_file($path)) {
Chris@5 69 throw new DriverException('File does not exist locally and cannot be uploaded to the remote instance.');
Chris@5 70 }
Chris@5 71
Chris@5 72 if (!class_exists('ZipArchive')) {
Chris@5 73 throw new DriverException('Could not compress file, PHP is compiled without zip support.');
Chris@5 74 }
Chris@5 75
Chris@5 76 // Selenium only accepts uploads that are compressed as a Zip archive.
Chris@5 77 $tempFilename = tempnam('', 'WebDriverZip');
Chris@5 78
Chris@5 79 $archive = new \ZipArchive();
Chris@5 80 $result = $archive->open($tempFilename, \ZipArchive::CREATE);
Chris@5 81 if (!$result) {
Chris@5 82 throw new DriverException('Zip archive could not be created. Error ' . $result);
Chris@5 83 }
Chris@5 84 $result = $archive->addFile($path, basename($path));
Chris@5 85 if (!$result) {
Chris@5 86 throw new DriverException('File could not be added to zip archive.');
Chris@5 87 }
Chris@5 88 $result = $archive->close();
Chris@5 89 if (!$result) {
Chris@5 90 throw new DriverException('Zip archive could not be closed.');
Chris@5 91 }
Chris@5 92
Chris@5 93 try {
Chris@5 94 $remotePath = $this->getWebDriverSession()->file(['file' => base64_encode(file_get_contents($tempFilename))]);
Chris@5 95
Chris@5 96 // If no path is returned the file upload failed silently.
Chris@5 97 if (empty($remotePath)) {
Chris@5 98 throw new UnknownError();
Chris@5 99 }
Chris@5 100 }
Chris@5 101 catch (\Exception $e) {
Chris@5 102 throw $e;
Chris@5 103 }
Chris@5 104 finally {
Chris@5 105 unlink($tempFilename);
Chris@5 106 }
Chris@5 107
Chris@5 108 return $remotePath;
Chris@5 109 }
Chris@5 110
Chris@0 111 }