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

How do I confirm a css element attribute with Capybara?

这可能看起来异常基本,但我如何确认弹出确认的存在?

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

<a 是 "tag"/"element" 而 data-confirm 是一个属性。我想测试 "data-confirm" 属性在 <a> element/tag

中是否存在

我试过了

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

但是页面源代码在那里显示了它并且它显然对用户有效

如有任何帮助,我们将不胜感激

你可以把这个期望写成:

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

Arup 的回答对于问题的标题是正确的(正如他在评论中所说,它只是有效的 CSS - https://developer.mozilla.org/en-US/docs/Web/CSS/Attribute_selectors),但是它实际上并没有测试更详细的部分问题 "how do I confirm the presence of a pop up confirmation"。它所做的只是确认正确的数据属性在 link 元素上以触发 rails 提供的 JS 应该显示确认。

如果你想实际测试显示的确认框,你需要切换到使用 JS 能力 driver - 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