我正在尝试在 Symfony 应用程序中使用 Selenium/Mink 和 Behat 来测试图像上传。该应用程序正在 Docker 容器中运行。
我将文件直接附加到输入的 NodeElement
,而不是使用 $driver->attachFileToField('#id-of-input', $filePath)
因为我们正在上下文中处理大量输入,并且在被调用的方法中已经有了输入:
$input->attachFile($this->filesPath . '1.jpg');
生成的路径是:
/var/www/html/src/Resources/TestImages/1.jpg
该文件确实存在于 docker 容器中的该路径中,但是当提交表单时,会抛出此错误:
没有有用的日志。
我尝试在 behat.yml
中设置 files_path
参数,但在测试运行期间出现错误:
unknown error: path is not absolute: 3.jpg
我错过了什么吗?该文件路径对于容器来说是否不正确?
我尝试在我的机器上使用abs路径,但没有成功(尽管这种方法有严重的缺点,所以我很高兴这不是解决方案):
/Users/its.me/Sites/kbs/src/Resources/TestImages/1.jpg
本地Users
目录也安装到我的docker机器中,以便abs路径在主机上工作。我认为这可能与权限相关,所以我将它们全部设置为读/写/执行,但没有雪茄!相对路径不起作用。
我的图片在哪里?
最佳答案
基于issue posted by @lauda at GitHub ,MinkSeleniumDriver 需要一些文件准备才能正常工作。即,将其转换为 zip 文件。 This comment帮助:
$localFile = $this->filesPath . '01.jpg';
$tempZip = tempnam('', 'WebDriverZip');
$zip = new \ZipArchive();
$zip->open($tempZip, \ZipArchive::CREATE);
$zip->addFile($localFile, basename($localFile));
$zip->close();
$remotePath = $this->getSession()->getDriver()->getWebDriverSession()->file([
'file' => base64_encode(file_get_contents($tempZip))
]);
$input->attachFile($remotePath);
unlink($tempZip);
以上代码基于facebook/php-webdriver
中的upload()
方法:
/**
* Upload a local file to the server
*
* @param string $local_file
*
* @throws WebDriverException
* @return string The remote path of the file.
*/
private function upload($local_file) {
if (!is_file($local_file)) {
throw new WebDriverException("You may only upload files: " . $local_file);
}
// Create a temporary file in the system temp directory.
$temp_zip = tempnam('', 'WebDriverZip');
$zip = new ZipArchive();
if ($zip->open($temp_zip, ZipArchive::CREATE) !== true) {
return false;
}
$info = pathinfo($local_file);
$file_name = $info['basename'];
$zip->addFile($local_file, $file_name);
$zip->close();
$params = array(
'file' => base64_encode(file_get_contents($temp_zip)),
);
$remote_path = $this->executor->execute(
DriverCommand::UPLOAD_FILE,
$params
);
unlink($temp_zip);
return $remote_path;
}
关于php - Behat Mink 文件上传在提交时未找到文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42114026/