首页 文章

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

提问于
浏览
0

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

我的一个步骤定义文件中的代码是使用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 回答

  • 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
    

相关问题