Commit f481697c authored by alexpott's avatar alexpott

Issue #2947517 by Lendude, jibran, alexpott, larowlan: Selenium driver: API to...

Issue #2947517 by Lendude, jibran, alexpott, larowlan: Selenium driver: API to get remote file paths

(cherry picked from commit 2c4ca068)
parent 6a466378
......@@ -3,6 +3,8 @@
namespace Drupal\FunctionalJavascriptTests;
use Behat\Mink\Driver\Selenium2Driver;
use Behat\Mink\Exception\DriverException;
use WebDriver\Exception\UnknownError;
use WebDriver\ServiceFactory;
/**
......@@ -41,4 +43,69 @@ public function setCookie($name, $value = NULL) {
$this->getWebDriverSession()->setCookie($cookieArray);
}
/**
* 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.
*
* @param string $path
* The path to the file to upload.
*
* @return string
* The remote path.
*
* @throws \Behat\Mink\Exception\DriverException
* When PHP is compiled without zip support, or the file doesn't exist.
* @throws \WebDriver\Exception\UnknownError
* When an unknown error occurred during file upload.
* @throws \Exception
* When a known error occurred during file upload.
*/
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;
}
}
<?php
namespace Drupal\FunctionalJavascriptTests\Tests;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
use Drupal\Tests\file\Functional\FileFieldCreationTrait;
use Drupal\Tests\TestFileCreationTrait;
/**
* Tests the DrupalSelenium2Driver methods.
*
* @coversDefaultClass \Drupal\FunctionalJavascriptTests\DrupalSelenium2Driver
* @group javascript
*/
class DrupalSelenium2DriverTest extends WebDriverTestBase {
use TestFileCreationTrait;
use FileFieldCreationTrait;
/**
* {@inheritdoc}
*/
protected static $modules = ['file', 'field_ui', 'entity_test'];
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$storage_settings = ['cardinality' => 3];
$this->createFileField('field_file', 'entity_test', 'entity_test', $storage_settings);
$this->drupalLogin($this->drupalCreateUser([
'administer entity_test content',
'access content',
]));
}
/**
* Tests uploading remote files.
*/
public function testGetRemoteFilePath() {
$web_driver = $this->getSession()->getDriver();
$file_system = \Drupal::service('file_system');
$entity = EntityTest::create();
$entity->save();
$files = array_slice($this->getTestFiles('text'), 0, 3);
$real_paths = [];
foreach ($files as $file) {
$real_paths[] = $file_system->realpath($file->uri);
}
$remote_paths = [];
foreach ($real_paths as $path) {
$remote_paths[] = $web_driver->uploadFileAndGetRemoteFilePath($path);
}
// Tests that uploading multiple remote files works with remote path.
$this->drupalGet($entity->toUrl('edit-form'));
$multiple_field = $this->xpath('//input[@multiple]')[0];
$multiple_field->setValue(implode("\n", $remote_paths));
$this->assertSession()->assertWaitOnAjaxRequest();
$this->getSession()->getPage()->findButton('Save')->click();
$entity = EntityTest::load($entity->id());
$this->assertCount(3, $entity->field_file);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment