编写了一个步骤定义,用于在集合中使用rspec检查CSS元素。如果一个期望失败了整个场景炸弹,我甚至没有得到关于每个CSS元素是否存在的独特反馈(不幸的是,它是全部或全部!我不希望这样)
我有以下代码:
Then(/^I should see the following css selectors:$/) do |table|
css_selectors = table.raw
css_selectors.each do |selector|
browser.element(:css=>selector.to_s).exists?.should be_true
end
end
如果我测试一大堆元素,即:
Then I should see the following css selectors:
|input#foo|
|.bar |
|h#baz |
|span.batz|
如果任何元素不存在,我会遇到问题。救命啊!
答案 0 :(得分:1)
您可以为包含失败的选择器的故障添加自定义消息。这是通过将第二个参数传递给should
方法来完成的:
Then(/^I should see the following css selectors:$/) do |table|
css_selectors = table.raw
css_selectors.each do |selector|
css_selector = selector[0].to_s
browser.element(:css=>css_selector).exists?.should be_true, "Element not found: #{css_selector}"
end
end
这会出现如下错误消息:
Element not found: input#foo (RSpec::Expectations::ExpectationNotMetError)
如果您还想确保运行所有选择器,则必须运行每个选择器,收集结果并对结果组执行断言。
Then(/^I should see the following css selectors:$/) do |table|
css_selectors = table.raw
# Collect any failed selectors
failed_selectors = []
css_selectors.each do |selector|
css_selector = selector[0].to_s
failed_selectors << css_selector unless browser.element(:css=>css_selector).exists?
end
# Check that none of the selectors failed
failed_selectors.should be_empty, "The following elements were not found #{failed_selectors.join(', ')}"
end
答案 1 :(得分:0)
你不能在场景中调用fail
吗?