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