如何使用水豚测试隐藏输入的文件附件?
How to test file attachment on hidden input using capybara?
我在标签内隐藏了输入:
<label for="upload">
<input class="hidden" type="file" name="file[picture]">
</label>
当我点击标签时,我附加了一个文件然后确认。
弹出模态 window 之后,我需要找到合适的 div class.
如何在水豚的帮助下进行测试?
您可以按照以下方式进行操作:
find('label[for=upload]').click
attach_file('input[name="file[picture]"]'), 'path/to/file.png')
within '.modal-popup' do
expect(page).to have_content '.divclass'
end
更新: Capybara 2.12 添加了一个 make_visible
选项到 attach_file
所以如果使用 2.12+ 你可以先尝试
attach_file('file[picture]', 'path/to/file.png', make_visible: true)
在直接使用 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
使用水豚“2.7.1”:
attach_file('file[picture]', 'path/to/file.png', visible: false)
我在标签内隐藏了输入:
<label for="upload">
<input class="hidden" type="file" name="file[picture]">
</label>
当我点击标签时,我附加了一个文件然后确认。
弹出模态 window 之后,我需要找到合适的 div class.
如何在水豚的帮助下进行测试?
您可以按照以下方式进行操作:
find('label[for=upload]').click
attach_file('input[name="file[picture]"]'), 'path/to/file.png')
within '.modal-popup' do
expect(page).to have_content '.divclass'
end
更新: Capybara 2.12 添加了一个 make_visible
选项到 attach_file
所以如果使用 2.12+ 你可以先尝试
attach_file('file[picture]', 'path/to/file.png', make_visible: true)
在直接使用 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
使用水豚“2.7.1”:
attach_file('file[picture]', 'path/to/file.png', visible: false)