how to force a cucumber scenario to fail?

前端 未结 2 1206
暗喜
暗喜 2020-12-20 17:47

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 fo

相关标签:
2条回答
  • 2020-12-20 18:13

    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
    
    0 讨论(0)
  • 2020-12-20 18:18

    Just a preliminary answer as I haven't been able to check it out yet, but I'd assume that calling Raise is always guaranteed to halt execution (unless it's done inside a proc or lambda so it's evaluation is deferred).

    Try simply

    if page.has_selector?(:dialog_message, 1, :text => 'Error')
        scenario.fail!(StandardError.new('Unexpected Error Dialog!'))
    end
    logout
    
    0 讨论(0)
提交回复
热议问题