如何使用水豚上传资产?
How to upload an asset using capybara?
我有
<input type="file" name="file" id="file" class="form-control-file teste" style="display: inline" ;="" accept=".pdf">
然后尝试
attach_file('file','PATH')
也试试
find('form input[type="file"]').set('PATH')
但我的结果是
cannot attach file, input[id=file] does not exist (Capybara::FileNotFound)
您发布的错误消息是当您尝试上传的文件不存在时返回的错误消息。
问题在于您不应将 input[id=file]
作为文件名传递。我不确定您使用的是哪个版本的 Capybara,但在当前版本中 attach_file
被定义为 def attach_file(locator = nil, paths, make_visible: nil, **options)
,其中定位器是文件输入的名称、ID 或相关标签文本,以及路径是要上传的文件的全名(或者可能是文件名数组)。问题在于,您显示的任何代码似乎都不会产生您声称的错误,除非您交换了参数,并且错误地传递了 CSS select或者而不是定位器。
注意:带有一些驱动程序(应该是任何支持 JS 的驱动程序)的 Capybara 3.15 支持 attach_file
的 beta 模式,您不必指定文件输入,而只需要与任何用户会触发文件 select,这在尝试使用隐藏文件输入元素的 UI 进行测试时会很有帮助
attach_file 'PATH' do
click_button 'Select File' # whatever action a user would do to select the file
end
我有
<input type="file" name="file" id="file" class="form-control-file teste" style="display: inline" ;="" accept=".pdf">
然后尝试
attach_file('file','PATH')
也试试
find('form input[type="file"]').set('PATH')
但我的结果是
cannot attach file, input[id=file] does not exist (Capybara::FileNotFound)
您发布的错误消息是当您尝试上传的文件不存在时返回的错误消息。
问题在于您不应将 input[id=file]
作为文件名传递。我不确定您使用的是哪个版本的 Capybara,但在当前版本中 attach_file
被定义为 def attach_file(locator = nil, paths, make_visible: nil, **options)
,其中定位器是文件输入的名称、ID 或相关标签文本,以及路径是要上传的文件的全名(或者可能是文件名数组)。问题在于,您显示的任何代码似乎都不会产生您声称的错误,除非您交换了参数,并且错误地传递了 CSS select或者而不是定位器。
注意:带有一些驱动程序(应该是任何支持 JS 的驱动程序)的 Capybara 3.15 支持 attach_file
的 beta 模式,您不必指定文件输入,而只需要与任何用户会触发文件 select,这在尝试使用隐藏文件输入元素的 UI 进行测试时会很有帮助
attach_file 'PATH' do
click_button 'Select File' # whatever action a user would do to select the file
end