使用Cucumber和Capybara,有没有一种方法来验证一个字符串不存在于页面上?黄瓜/水豚:检查一个页面没有内容?
例如,我将如何写此步骤的相反:
Then /^I should see "(.*?)"$/ do |arg1|
page.should have_content(arg1)
end
此传递如果arg1
存在。
如何找到失败的步骤如果找到arg1
?
使用Cucumber和Capybara,有没有一种方法来验证一个字符串不存在于页面上?黄瓜/水豚:检查一个页面没有内容?
例如,我将如何写此步骤的相反:
Then /^I should see "(.*?)"$/ do |arg1|
page.should have_content(arg1)
end
此传递如果arg1
存在。
如何找到失败的步骤如果找到arg1
?
中有水豚一个has_no_content
匹配。所以,你可以写
Then /^I should not see "(.*?)"$/ do |arg1|
page.should have_no_content(arg1)
end
哦,等等,我想通了。这工作:
Then /^I should see "(.*?)"$/ do |arg1|
page.has_content?(arg1) == false
end
您的代码不主张的内容。我想如果你改变错误为真,你的测试仍然会通过。 – 2013-09-04 15:28:12
这让我绊了好几次。要么使用'assert page.has_content ...'或'page.should have_content ...'不能将两个样式混合在一起。好电话,@PhilippeRathé。 – Tass 2015-10-09 17:10:44
您还可以,如果你想要读好一点的使用should_not:
Then /^I should not see "(.*?)"$/ do |arg1|
page.should_not have_content(arg1)
end
一些更多的信息:https://www.relishapp.com/rspec/rspec-expectations/docs
请注意,这将只适用于JavaScript测试,如果你有你的规格包括Capybara :: RSpecMatchers。我只是遇到了这个问题:http://stackoverflow.com/a/15253235/125377 – Ari 2013-03-06 21:02:26
改为使用'has_no_content?'。这将触发水豚的内部等待。否则,如果您等待内容从页面中消失,则会过早失败。 – keithepley 2013-08-20 19:31:20
,你可以使用:
Then /^I not see "(.*?)"$/ do |arg1|
expect(page).to have_no_content(arg1)
end
如果在页面中找到内容,测试是红色
这是正确的语法。如果您使用的是expect(),那么我会重新说明您的规范,从描述中删除“should”。更自信! :) – Rimian 2015-12-01 00:31:42
谢谢@Rimian。我修好了! – 2016-03-23 14:01:51
在Rspec的3.4目前(2016),这是测试没有内容的推荐方法:
expect(page).not_to have_content(arg1)
你认为在'expect(page).to have_no_content(arg1)''上推荐这么做?似乎没有办法让not_to知道不要等待页面上的内容,这就是'have_content'会做的事情,我想。 – Ibrahim 2016-12-01 01:05:57
弃用警告在使用'expect(page).to have_no_content(arg1)' – 2016-12-18 17:32:08
@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')' – seanriordan08 2017-04-21 13:27:06
我很惊讶这比'assert!page.has_content?(arg)' – 2015-03-11 14:43:18
@MichaelDiscenza快多少,你是对的,查看更多信息 - https://github.com/ngauthier/capybara-slow_finder_errors – 2016-01-07 18:08:47