Skip to content

Instantly share code, notes, and snippets.

@matt-halliday
Created September 20, 2018 12:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matt-halliday/fd7ebb6e8ab49776dffb8e55dde4e0c8 to your computer and use it in GitHub Desktop.
Save matt-halliday/fd7ebb6e8ab49776dffb8e55dde4e0c8 to your computer and use it in GitHub Desktop.
Append files to file input in Behat with Selenium2 Driver
/**
* @Given I attach the file :path
* @param string $path
*/
public function iAttachTheFile(string $path)
{
// Get absolute local path
$localFile = '/var/www/html/' . $path;
// Convert to zip file
$tempZip = tempnam('', 'WebDriverZip');
$zip = new \ZipArchive();
$zip->open($tempZip, \ZipArchive::CREATE);
$zip->addFile($localFile, basename($localFile));
$zip->close();
$remotePath = $this->minkContext->getSession()->getDriver()->getWebDriverSession()->file([
'file' => base64_encode(file_get_contents($tempZip))
]);
$input = $this->minkContext->getSession()->getPage()->find('xpath', '//input[@type=\'file\']');
$input->attachFile($remotePath);
unlink($tempZip);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment