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 arg1
is present.
如果arg1存在,则通过。
How would I write a step that fails if arg1
is found?
如果发现arg1,我该如何编写一个失败的步骤?
5 个解决方案
#1
24
http://rubydoc.info/github/jnicklas/capybara/master/Capybara/Node/Matchers has_no_text f-instance_method % 3
There is a has_no_content
matcher in Capybara. So you can write
在Capybara有一个has_no_content matcher。所以你可以写
Then /^I should not see "(.*?)"$/ do |arg1|
page.should have_no_content(arg1)
end
#2
11
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)
#3
8
You can also use should_not if 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
#4
4
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
如果在页面中找到内容,您的测试是红色的
#5
-3
Oh, wait, I figured it out. This works:
等等,我想出来了。如此:
Then /^I should see "(.*?)"$/ do |arg1|
page.has_content?(arg1) == false
end
#1
24
http://rubydoc.info/github/jnicklas/capybara/master/Capybara/Node/Matchers has_no_text f-instance_method % 3
There is a has_no_content
matcher in Capybara. So you can write
在Capybara有一个has_no_content matcher。所以你可以写
Then /^I should not see "(.*?)"$/ do |arg1|
page.should have_no_content(arg1)
end
#2
11
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)
#3
8
You can also use should_not if 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
#4
4
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
如果在页面中找到内容,您的测试是红色的
#5
-3
Oh, wait, I figured it out. This works:
等等,我想出来了。如此:
Then /^I should see "(.*?)"$/ do |arg1|
page.has_content?(arg1) == false
end