[英]How to fail a step in step definition file so that scenario is automatically marked as failed in cucumber-capybara environment?
有没有简单的方法可以将步骤标记为在黄瓜中失败,从而使场景在黄瓜中被标记为失败?
我的步骤定义文件之一中的代码是使用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服务器无法启动”行之后在输出中得到这些行。
还有其他简单方法可以使步骤定义文件中的步骤失败吗?
多余的行是您引发的异常的堆栈跟踪。 如果指定异常类型,则应该能够覆盖stacktrace。 另外,这种行为就是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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.