Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.4k views
in Technique[技术] by (71.8m points)

ruby - how to force a cucumber scenario to fail?

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can get the after hook to fail using your normal assertions. Have not done 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 do the scenario.fail!(), you will still not logout. You need to wrap it in a begin-ensure block.

Try this:

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

Update

If you do not want to call the standard assertions and directly fail the scenario, 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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...