如何使步骤定义文件中的步骤失败,以便在cucumber-capybara环境中自动将场景标记为失败?

时间:2017-02-21 22:38:30

标签: ruby cucumber capybara

是否有任何简单的方法可以将步骤标记为黄瓜失败,以便黄瓜中的情景被标记为失败?

我的一个步骤定义文件中的代码是使用Ruby语言编写的:

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

当我运行我的功能文件时,我得到了这个输出 Output_Snapshot

我不明白为什么在“Web服务器无法启动”之后我在输出中获得这些行。

是否有其他简单方法可以使步骤定义文件中的步骤失败?

1 个答案:

答案 0 :(得分:2)

额外的行是你引发的异常的堆栈跟踪。如果指定了异常类型,则应该能够覆盖堆栈跟踪。此外,这种行为是retry声明用于

的行为
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