简体   繁体   中英

How to fail a step in step definition file so that scenario is automatically marked as failed in cucumber-capybara environment?

Is there any simple way to mark the step as failed in cucumber so that scenario gets marked as failed in cucumber?

My code in one of my step definition file which is written using Ruby language:

SECONDS_TO_SLEEP = 5
MAX_NUM_ITERATIONS = 3

Given(/^The TREC UI is running$/) do
    elapsedTime = 1
    currentAttempts = 0
    while currentAttempts <= MAX_NUM_ITERATIONS
        begin
            visit "http://sut:8080/myPage"
            expect(page).to have_content("Welcome to My Page")
            totalTime = elapsedTime + currentAttempts * SECONDS_TO_SLEEP
            puts "It took #{totalTime} seconds for TREC UI to come up."
            break

        rescue
            currentAttempts += 1
            sleep SECONDS_TO_SLEEP
            if currentAttempts <= MAX_NUM_ITERATIONS
                puts "Waiting for web server to start."
            else
                raise "Web server failed to start."
            end
        end
    end
end

When I run my feature file, I get the this output Output_Snapshot

I do not understand why do I get those lines in the output after line "Web server failed to start".

Is there any other simple way to fail the step in the step definition file?

The extra lines are the stacktrace from the exception you raise. You should be able to override the stacktrace if you specify an exception type. Also, this type of behavior is what the retry statement is for

Given(/^The TREC UI is running$/) do
  elapsedTime = 1
  currentAttempts = 0
  begin
    visit "http://sut:8080/myPage"
    puts "Waiting for web server to start."
    expect(page).to have_content("Welcome to My Page", wait: SECONDS_TO_SLEEP)
    totalTime = elapsedTime + currentAttempts * SECONDS_TO_SLEEP # This needs some math fixup if using the wait option above - won't work with rack_test driver though
    puts "It took #{totalTime} seconds for TREC UI to come up."
  rescue
    retry if (currentAttempts += 1) <= MAX_NUM_ITERATIONS
    # If using the rack_test driver then the :wait option above won't
    # work so you would need to use sleep like below instead of the line above
    # if (currentAttempts += 1) <= MAX_NUM_ITERATIONS
    #   sleep SECONDS_TO_SLEEP
    #   retry
    # end
    raise RuntimeError, "Web server failed to start.", []  # The [] overrides the stack info with an empty array
  end
end 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM