Chris@14: setServiceClass('service.curl', WebDriverCurlService::class); Chris@17: } Chris@17: Chris@17: /** Chris@17: * {@inheritdoc} Chris@17: */ Chris@14: public function setCookie($name, $value = NULL) { Chris@14: if ($value === NULL) { Chris@14: $this->getWebDriverSession()->deleteCookie($name); Chris@14: return; Chris@14: } Chris@14: Chris@14: $cookieArray = [ Chris@14: 'name' => $name, Chris@14: 'value' => urlencode($value), Chris@14: 'secure' => FALSE, Chris@14: // Unlike \Behat\Mink\Driver\Selenium2Driver::setCookie we set a domain Chris@14: // and an expire date, as otherwise cookies leak from one test site into Chris@14: // another. Chris@14: 'domain' => parse_url($this->getWebDriverSession()->url(), PHP_URL_HOST), Chris@14: 'expires' => time() + 80000, Chris@14: ]; Chris@14: Chris@14: $this->getWebDriverSession()->setCookie($cookieArray); Chris@14: } Chris@14: Chris@18: /** Chris@18: * Uploads a file to the Selenium instance and returns the remote path. Chris@18: * Chris@18: * \Behat\Mink\Driver\Selenium2Driver::uploadFile() is a private method so Chris@18: * that can't be used inside a test, but we need the remote path that is Chris@18: * generated when uploading to make sure the file reference exists on the Chris@18: * container running selenium. Chris@18: * Chris@18: * @param string $path Chris@18: * The path to the file to upload. Chris@18: * Chris@18: * @return string Chris@18: * The remote path. Chris@18: * Chris@18: * @throws \Behat\Mink\Exception\DriverException Chris@18: * When PHP is compiled without zip support, or the file doesn't exist. Chris@18: * @throws \WebDriver\Exception\UnknownError Chris@18: * When an unknown error occurred during file upload. Chris@18: * @throws \Exception Chris@18: * When a known error occurred during file upload. Chris@18: */ Chris@18: public function uploadFileAndGetRemoteFilePath($path) { Chris@18: if (!is_file($path)) { Chris@18: throw new DriverException('File does not exist locally and cannot be uploaded to the remote instance.'); Chris@18: } Chris@18: Chris@18: if (!class_exists('ZipArchive')) { Chris@18: throw new DriverException('Could not compress file, PHP is compiled without zip support.'); Chris@18: } Chris@18: Chris@18: // Selenium only accepts uploads that are compressed as a Zip archive. Chris@18: $tempFilename = tempnam('', 'WebDriverZip'); Chris@18: Chris@18: $archive = new \ZipArchive(); Chris@18: $result = $archive->open($tempFilename, \ZipArchive::CREATE); Chris@18: if (!$result) { Chris@18: throw new DriverException('Zip archive could not be created. Error ' . $result); Chris@18: } Chris@18: $result = $archive->addFile($path, basename($path)); Chris@18: if (!$result) { Chris@18: throw new DriverException('File could not be added to zip archive.'); Chris@18: } Chris@18: $result = $archive->close(); Chris@18: if (!$result) { Chris@18: throw new DriverException('Zip archive could not be closed.'); Chris@18: } Chris@18: Chris@18: try { Chris@18: $remotePath = $this->getWebDriverSession()->file(['file' => base64_encode(file_get_contents($tempFilename))]); Chris@18: Chris@18: // If no path is returned the file upload failed silently. Chris@18: if (empty($remotePath)) { Chris@18: throw new UnknownError(); Chris@18: } Chris@18: } Chris@18: catch (\Exception $e) { Chris@18: throw $e; Chris@18: } Chris@18: finally { Chris@18: unlink($tempFilename); Chris@18: } Chris@18: Chris@18: return $remotePath; Chris@18: } Chris@18: Chris@14: }