我有以下规格:
within dom_id_selector(@boilerplate_copy) do
within '.copied_attributes_differing_from_original' do
within 'form.title[action="http://www.textdiff.com/"]' do
expect(page).to have_css 'button', text: 'Title'
expect(page).to have_css 'input[name="string1"]'
expect(page).to have_css 'input[name="string2"]'
end
end
end
以下是有问题的HTML:
<div class="copied_attributes_differing_from_original">
<form action="http://www.textdiff.com/" class="title" method="post" target="_blank">
<input name="string1" type="hidden" value="Boilerplate Original test title">
<input name="string2" type="hidden" value="Boilerplate Copy test title">
<button type="submit">Title</button>
</form>
<form action="http://www.textdiff.com/" class="success_criterion_id" method="post" target="_blank">
<input name="string1" type="hidden">
<input name="string2" type="hidden" value="1">
<button type="submit">Success criterion</button>
</form>
// More of them...
<form action="http://www.textdiff.com/"...
</div>
当第一个have_css
次通过(找到按钮)时,第二个have_css
失败:
Failure/Error: expect(page).to have_css 'input[name="string1"]'
expected to find css "input[name=\"string1\"]" but there were no matches. Also found "", which matched the selector but not all filters.
但在我看来,这个元素绝对存在!我也不理解输出Also found "", which matched the selector but not all filters.
,它是什么意思?
谢谢。
答案 0 :(得分:3)
默认情况下,Capybara找不到不可见的元素(隐藏的输入是),因为用户无法查看/与之交互。如果你真的需要检查是否存在不可见的元素,你可以
expect(page).to have_css('input[name="string1"]', visible: false)
话虽这么说,因为功能测试应该测试页面功能的工作而不是它的确切工作原理的细节,你可能不想检查是否存在隐藏的输入,而只是确保它们实现的功能有效。