如何强迫黄瓜方案失败?

有没有办法迫使黄瓜情景失败?

我需要在每次测试结束时检查一些失败的情况。 所以我认为我可以检查“错误”对话框,然后如果它发生则测试失败。

这可以通过下面的代码实现,但是存在问题。 一旦我在失败中提出exception! 函数,然后黄瓜停止运行After钩子的其余部分,因此注销函数不会被调用。

是:

After() do |scenario| #Checking for Error popups if page.has_selector?(:dialog_message, 1, :text => 'Error') fail!(raise(ArgumentError.new('Unexpected Error dialog!'))) end logout end 

现在:

 After() do |scenario| #Checking for Error popups if page.has_selector?(:dialog_message, 1, :text => 'Error') scenario.fail!(logout) end end 

是否有更好的方法可以在不引发exception的情况下通过黄瓜测试失败?

您可以使用正常断言来获取挂钩失败。 没有做过Capybara / rspecexception,但我认为你可以这样做:

 page.should have_selector?(:dialog_message, 1, :text => 'Error') 

但是,如果您执行此操作或执行scenario.fail!(),您仍然无法注销。 您需要将其包装在begin-ensure块中。

试试这个:

 After do |scenario| begin page.should have_selector?(:dialog_message, 1, :text => 'Error') ensure logout end end 

更新

如果您不想调用标准断言并直接使场景失败,您可以执行以下操作 – 您需要使用fail而不是fail!

 After() do |scenario| begin #Checking for Error popups if page.has_selector?(:dialog_message, 1, :text => 'Error') fail(ArgumentError.new('Unexpected Error dialog!')) #Or you can just do fail('Unexpected Error dialog') if you do not care about the type. end ensure logout end end 

只是一个初步的答案,因为我还没有能够检查出来,但我认为调用Raise总是保证停止执行(除非它在proc或lambda中完成,所以它的评估被推迟)。

试试吧

 if page.has_selector?(:dialog_message, 1, :text => 'Error') scenario.fail!(StandardError.new('Unexpected Error Dialog!')) end logout