function Selenium2Driver::uploadFile
Uploads a file to the Selenium instance.
Note that uploading files is not part of the official WebDriver specification, but it is supported by Selenium.
Parameters
string $path The path to the file to upload.:
Return value
string The remote path.
Throws
DriverException When PHP is compiled without zip support, or the file doesn't exist.
UnknownError When an unknown error occurred during file upload.
\Exception When a known error occurred during file upload.
See also
https://github.com/SeleniumHQ/selenium/blob/master/py/selenium/webdrive…
1 call to Selenium2Driver::uploadFile()
- Selenium2Driver::attachFile in vendor/
lullabot/ mink-selenium2-driver/ src/ Selenium2Driver.php - Attaches file path to file field located by its XPath query.
File
-
vendor/
lullabot/ mink-selenium2-driver/ src/ Selenium2Driver.php, line 1320
Class
- Selenium2Driver
- Selenium2 driver.
Namespace
Behat\Mink\DriverCode
private function uploadFile(string $path) : string {
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.');
}
// Selenium only accepts uploads that are compressed as a Zip archive.
$tempFilename = tempnam('', 'WebDriverZip');
if ($tempFilename === false) {
throw new DriverException('Could not create a temporary file.');
}
$archive = new \ZipArchive();
$result = $archive->open($tempFilename, \ZipArchive::OVERWRITE);
if ($result !== true) {
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.');
}
$fileContents = file_get_contents($tempFilename);
\assert($fileContents !== false);
try {
$remotePath = $this->getWebDriverSession()
->file(array(
'file' => base64_encode($fileContents),
));
// If no path is returned the file upload failed silently. In this
// case it is possible Selenium was not used but another web driver
// such as PhantomJS.
// @todo Support other drivers when (if) they get remote file transfer
// capability.
if (empty($remotePath)) {
throw new UnknownError();
}
} finally {
unlink($tempFilename);
}
return $remotePath;
}