Ruby-on-rails 黄瓜/水豚:检查页面是否没有内容?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11980109/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 03:51:48  来源:igfitidea点击:

Cucumber/Capybara: check that a page does NOT have content?

ruby-on-railscucumbercapybara

提问by dB'

Using Cucumber and Capybara, is there a way to verify that a string is NOT present on a page?

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

For example, how would I write the opposite of this step:

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

Then /^I should see "(.*?)"$/ do |arg1|
  page.should have_content(arg1)
end

This passes if arg1is present.

如果arg1存在,则通过。

How would I write a step that failsif arg1is found?

如果找到,我将如何编写失败的步骤arg1

回答by Piotr Jakubowski

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

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

There is a has_no_contentmatcher in Capybara. So you can write

has_no_content水豚有一个匹配器。所以你可以写

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

回答by Micah

In Rspec 3.4 currently (2016) this is the recommended way to test for not having content:

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

expect(page).not_to have_content(arg1)

回答by Adam Sheehan

You can also use should_notif you want it read a little better:

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

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

Some more info: https://www.relishapp.com/rspec/rspec-expectations/docs

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

回答by Raphael Abreu

currently, you can use:

目前,您可以使用:

Then /^I not see "(.*?)"$/ do |arg1|
  expect(page).to have_no_content(arg1)
end

And if the content is found in the page, your test is red

如果在页面中找到内容,则您的测试为红色

回答by dB'

Oh, wait, I figured it out. This works:

哦,等等,我想通了。这有效:

Then /^I should see "(.*?)"$/ do |arg1|
  page.has_content?(arg1) == false
end