Cucumber/Capybara:检查页面没有内容?

dB'*_*dB' 18 ruby-on-rails cucumber capybara

使用Cucumber和Capybara,有没有办法验证页面上是否存在字符串?

例如,我将如何编写与此步骤相反的内容:

Then /^I should see "(.*?)"$/ do |arg1|
  page.should have_content(arg1)
end
Run Code Online (Sandbox Code Playgroud)

如果arg1存在则通过.

如果找到,我怎么写一个失败的步骤arg1

Pio*_*ski 29

http://rubydoc.info/github/jnicklas/capybara/master/Capybara/Node/Matchers#has_no_text%3F-instance_method

has_no_contentCapybara 有一个匹配器.所以你可以写

  Then /^I should not see "(.*?)"$/ do |arg1|
    page.should have_no_content(arg1)
  end
Run Code Online (Sandbox Code Playgroud)


Mic*_*cah 13

在目前(2016年)的Rspec 3.4中,这是测试没有内容的推荐方法:

expect(page).not_to have_content(arg1)
Run Code Online (Sandbox Code Playgroud)

  • @RobHughes,您所说的折旧警告必须已在 rspec 3.5.4/capybara 2.6.2 中删除。根据文档:https://github.com/teamcapybara/capybara#asynchronous-javascript-ajax-and-friends - 然而,Capybara 的 RSpec 匹配器足够智能,可以处理任何一种形式。以下两个语句在功能上是等效的:expect(page).not_to have_xpath('a')、expect(page).to have_no_xpath('a')` (2认同)

Ada*_*han 8

如果你想让它读得更好,你也可以使用should_not:

Then /^I should not see "(.*?)"$/ do |arg1|
  page.should_not have_content(arg1)
end
Run Code Online (Sandbox Code Playgroud)

更多信息:https://www.relishapp.com/rspec/rspec-expectations/docs

  • 请改用"has_no_content?".这将触发Capybara的内部等待.否则,如果您正在等待内容从页面中消失,它将过早失败. (13认同)