如何使用Capybara确认css元素属性?

时间:2018-01-20 04:09:50

标签: ruby-on-rails rspec capybara

这可能看起来非常基本,但如何确认是否存在弹出确认?

<a data-confirm="delete this video?" rel="nofollow" data-method="delete" href="/videos/21">Delete</a>

<a是“标记”/“元素”,data-confirm是属性。我想测试<a>元素/标记

中是否存在“data-confirm”属性

我试过了

expect(page).to have_css("a.data-confirm.delete this video?")

capybara assert attributes of an element 但没有快乐。

编辑:

我已经尝试过Arup在下面评论的期望

expect(page).to have_content "Content"
click_link "Delete"
expect(page).to have_css('a[data-confirm="delete this video?"]')

但它引发了以下(相同)错误

Failures:

1) Visiting the video index page should search and save movies
 Failure/Error: expect(page).to have_css('a[data-confirm="delete this video?"]')
   expected #has_css?("a[data-confirm=\"delete this video?\"]") to return true, got false

但页面来源显示了它,它显然适用于用户

非常感谢任何帮助

2 个答案:

答案 0 :(得分:2)

您可以将此期望写为:

expect(page).to have_css('a[data-confirm="delete this video?"]')

答案 1 :(得分:0)

Arup的答案对于问题的标题是正确的(正如他在评论中所说的那样,它只是有效的CSS - https://developer.mozilla.org/en-US/docs/Web/CSS/Attribute_selectors),但它实际上并没有测试问题的更详细部分“怎么做我确认存在弹出确认“。它所做的就是确认链接元素上的正确数据属性是否触发了应该显示确认的JS提供的轨道。

如果您想要实际测试确认框,则需要交换使用支持JS的驱动程序 - https://github.com/teamcapybara/capybara/tree/2.17_stable#drivers - 然后在测试中使用以下内容

expect(page).to have_content "Content"
accept_confirm "delete this video?" do
  click_link "Delete" # The action that will make the system modal confirm box appear
end

请参阅 - http://www.rubydoc.info/gems/capybara/Capybara/Session#accept_confirm-instance_method

相关问题