function DrupalSelenium2Driver::uploadFileAndGetRemoteFilePath

Same name in other branches
  1. 9 core/tests/Drupal/FunctionalJavascriptTests/DrupalSelenium2Driver.php \Drupal\FunctionalJavascriptTests\DrupalSelenium2Driver::uploadFileAndGetRemoteFilePath()
  2. 10 core/tests/Drupal/FunctionalJavascriptTests/DrupalSelenium2Driver.php \Drupal\FunctionalJavascriptTests\DrupalSelenium2Driver::uploadFileAndGetRemoteFilePath()
  3. 11.x core/tests/Drupal/FunctionalJavascriptTests/DrupalSelenium2Driver.php \Drupal\FunctionalJavascriptTests\DrupalSelenium2Driver::uploadFileAndGetRemoteFilePath()

Uploads a file to the Selenium instance and returns the remote path.

\Behat\Mink\Driver\Selenium2Driver::uploadFile() is a private method so that can't be used inside a test, but we need the remote path that is generated when uploading to make sure the file reference exists on the container running selenium.

Parameters

string $path: The path to the file to upload.

Return value

string The remote path.

Throws

\Behat\Mink\Exception\DriverException When PHP is compiled without zip support, or the file doesn't exist.

\WebDriver\Exception\UnknownError When an unknown error occurred during file upload.

\Exception When a known error occurred during file upload.

File

core/tests/Drupal/FunctionalJavascriptTests/DrupalSelenium2Driver.php, line 67

Class

DrupalSelenium2Driver
Provides a driver for Selenium testing.

Namespace

Drupal\FunctionalJavascriptTests

Code

public function uploadFileAndGetRemoteFilePath($path) {
    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');
    $archive = new \ZipArchive();
    $result = $archive->open($tempFilename, \ZipArchive::CREATE);
    if (!$result) {
        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.');
    }
    try {
        $remotePath = $this->getWebDriverSession()
            ->file([
            'file' => base64_encode(file_get_contents($tempFilename)),
        ]);
        // If no path is returned the file upload failed silently.
        if (empty($remotePath)) {
            throw new UnknownError();
        }
    } catch (\Exception $e) {
        throw $e;
    } finally {
        unlink($tempFilename);
    }
    return $remotePath;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.