How to make a cucumber script fail?

Is there any way to collapse the cucumber script?

I need to check for some unsuccessful scripts at the end of each of my tests. So I thought I could do a check for the error dialog and then skip the test if it happened.

This is possible using the code below, however there is a problem. As soon as I throw an exception to fail! function, then the cucumber stops running the rest of After hook, so the exit function is not called.

Was:

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 

Now:

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

Is there a better way to skip a cucumber test without raising an exception?

+7
source share
2 answers

You can get rejected after the hook using your usual statements. Did not do much with Capybara / rspec exceptions, but I think you can do:

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

However, if you do this or run the .fail! () Script, you still will not be logged out. You need to wrap it in a start-guarantee block.

Try the following:

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

Update

If you do not want to invoke standard statements and directly fail, you can do the following: you need to use fail instead of 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 
+8
source

Just a preliminary answer, since I have not been able to check it yet, but I would assume that calling Raise always be guaranteed to stop executing (if this is not done inside proc or lambda, so the evaluation is deferred).

Just try

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

All Articles