how to force a cucumber scenario to fail?

Viewed 16198

Is there a way to force a cucumber scenario to fail?

I need to check for a few failing scenarios at the end of each of my tests. So I thought I could do the check for an 'error' dialog and then fail the test if it occurred.

This is possible with the code below however there is a problem. Once I raise the exception in the fail! function, then cucumber stops running the rest of the After hook, so the logout function doesnt get 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 fail a cucumber test without raising an exception?

2 Answers
Related