comparison 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
comparison
equal deleted inserted replaced
4:a9cd425dd02b 5:12f9dff5fda9
1 <?php 1 <?php
2 2
3 namespace Drupal\FunctionalJavascriptTests; 3 namespace Drupal\FunctionalJavascriptTests;
4 4
5 use Behat\Mink\Driver\Selenium2Driver; 5 use Behat\Mink\Driver\Selenium2Driver;
6 use Behat\Mink\Exception\DriverException;
7 use WebDriver\Exception\UnknownError;
6 use WebDriver\ServiceFactory; 8 use WebDriver\ServiceFactory;
7 9
8 /** 10 /**
9 * Provides a driver for Selenium testing. 11 * Provides a driver for Selenium testing.
10 */ 12 */
39 ]; 41 ];
40 42
41 $this->getWebDriverSession()->setCookie($cookieArray); 43 $this->getWebDriverSession()->setCookie($cookieArray);
42 } 44 }
43 45
46 /**
47 * Uploads a file to the Selenium instance and returns the remote path.
48 *
49 * \Behat\Mink\Driver\Selenium2Driver::uploadFile() is a private method so
50 * that can't be used inside a test, but we need the remote path that is
51 * generated when uploading to make sure the file reference exists on the
52 * container running selenium.
53 *
54 * @param string $path
55 * The path to the file to upload.
56 *
57 * @return string
58 * The remote path.
59 *
60 * @throws \Behat\Mink\Exception\DriverException
61 * When PHP is compiled without zip support, or the file doesn't exist.
62 * @throws \WebDriver\Exception\UnknownError
63 * When an unknown error occurred during file upload.
64 * @throws \Exception
65 * When a known error occurred during file upload.
66 */
67 public function uploadFileAndGetRemoteFilePath($path) {
68 if (!is_file($path)) {
69 throw new DriverException('File does not exist locally and cannot be uploaded to the remote instance.');
70 }
71
72 if (!class_exists('ZipArchive')) {
73 throw new DriverException('Could not compress file, PHP is compiled without zip support.');
74 }
75
76 // Selenium only accepts uploads that are compressed as a Zip archive.
77 $tempFilename = tempnam('', 'WebDriverZip');
78
79 $archive = new \ZipArchive();
80 $result = $archive->open($tempFilename, \ZipArchive::CREATE);
81 if (!$result) {
82 throw new DriverException('Zip archive could not be created. Error ' . $result);
83 }
84 $result = $archive->addFile($path, basename($path));
85 if (!$result) {
86 throw new DriverException('File could not be added to zip archive.');
87 }
88 $result = $archive->close();
89 if (!$result) {
90 throw new DriverException('Zip archive could not be closed.');
91 }
92
93 try {
94 $remotePath = $this->getWebDriverSession()->file(['file' => base64_encode(file_get_contents($tempFilename))]);
95
96 // If no path is returned the file upload failed silently.
97 if (empty($remotePath)) {
98 throw new UnknownError();
99 }
100 }
101 catch (\Exception $e) {
102 throw $e;
103 }
104 finally {
105 unlink($tempFilename);
106 }
107
108 return $remotePath;
109 }
110
44 } 111 }