Behat貂皮file upload找不到提交文件
我试图在Symfony应用程序中使用Sehat来testing使用Selenium / Mink的Behat。 该应用程序正在Docker容器中运行。
我将文件直接附加到input的NodeElement
,而不是使用$driver->attachFileToField('#id-of-input', $filePath)
因为我们正在处理上下文中的大量input,该方法中的input被调用:
$input->attachFile($this->filesPath . '1.jpg');
由此产生的path是:
/var/www/html/src/Resources/TestImages/1.jpg
该文件当然存在于Docker容器中的path中,但是当提交表单时,会抛出以下错误:
没有有用的日志。
我已经尝试在files_path
中设置files_path
参数,但是在testing运行期间出现错误:
unknown error: path is not absolute: 3.jpg
我错过了什么吗? 该容器的文件path是否不正确?
我试过在我的机器上使用的abspath无济于事(虽然这种方法有严重的缺点,所以我很高兴这不是解决scheme):
/Users/its.me/Sites/kbs/src/Resources/TestImages/1.jpg
本地Users
目录也被安装到我的docker机器,以便abspath在主机上工作。 我认为这可能是权限相关,所以我把他们都读/写/执行,但没有雪茄! 相对path不起作用。
我的图像在哪里?
根据@lauda在GitHub发布的问题 ,MinkSeleniumDriver需要一些文件准备才能正常工作。 即把它变成一个zip文件。 这个评论帮助:
$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; }
将文件添加到behat目录下,并在files_path
Behat\MinkExtension\Extension:
下设置files_path
Behat\MinkExtension\Extension:
files_path
应该是这样的: files_path: %behat.paths.features%/bootstrap
确保在MinkExtension
部分的Behat.yml
文件中包含files_path
,例如
default: extensions: Behat\MinkExtension\Extension: files_path: %behat.paths.base%/build/dummy/
还要确保你有正确的文件夹结构,例如
# Your application football build dummy document hello.doc world.xls image test.jpg
示例Behatfunction:
Feature: Example feature Scenario: I can upload image Given I am on "/" When I attach the file "image/test.jpg" to "league_flag" And I press "Submit" Then I should see "Succeeded."
这里是实际的MinkExtension构build方法:
/** * Attaches file to field with specified id|name|label|value * Example: When I attach "bwayne_profile.png" to "profileImageUpload" * Example: And I attach "bwayne_profile.png" to "profileImageUpload" * * @When /^(?:|I )attach the file "(?P<path>[^"]*)" to "(?P<field>(?:[^"]|\\")*)"$/ */ public function attachFileToField($field, $path) { $field = $this->fixStepArgument($field); if ($this->getMinkParameter('files_path')) { $fullPath = rtrim(realpath($this->getMinkParameter('files_path')), DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR.$path; if (is_file($fullPath)) { $path = $fullPath; } } $this->getSession()->getPage()->attachFileToField($field, $path); }
请参阅: 使用Behat上传文件 。