如何使用capybara测试隐藏输入上的文件附件?

Bil*_*gan 9 rspec ruby-on-rails capybara

我在标签内隐藏了输入:

<label for="upload">
  <input class="hidden" type="file" name="file[picture]">
</label>
Run Code Online (Sandbox Code Playgroud)

当我点击标签时,我附上一个文件,然后确认.

弹出模态窗口后,我需要找到合适的div类.

我怎样才能在水豚的帮助下测试这个?

Tho*_*ole 26

更新: Capybara 2.12添加了一个make_visible选项,attach_file所以如果使用2.12+你可以先尝试

attach_file('file[picture]', 'path/to/file.png', make_visible: true)
Run Code Online (Sandbox Code Playgroud)

在直接使用execute_script自己之前


文件输入是一种特殊情况,因为它们经常因样式原因而隐藏,并使用系统模式进行交互.Capybara使得很难填充页面上的隐藏字段,因为用户通常无法与它们交互,因此对于文件输入,通常的做法是使用execute_script它们使其可见,然后填充它们.

execute_script("$('input[name=\"file[picture]\"]').removeClass('hidden')") # assumes you have jQuery available - if not change to valid JS for your environment
attach_file('file[picture]', 'path/to/file.png') # takes id, name or label text of field not a random selector
Run Code Online (Sandbox Code Playgroud)


Kuf*_*Kuf 7

使用Capybara'2.7.1':

attach_file('file[picture]', 'path/to/file.png', visible: false)
Run Code Online (Sandbox Code Playgroud)