我正在编写一个检查超时异常的方案。这是我的Ruby代码:
# Execute the constructed command, logging out each line
log.info "Executing '#{command.join(' ')}'"
begin
timeout(config['deploy-timeout'].to_i) do
execute_and_log command
end
rescue Exception => e
log.info 'Err, timeout, ouch'
raise Timeout::Error
end
我想检查Err的输出,超时或者是否在Gherkin / Cucumber中引发异常。
Then(/^the output should be 'Err, timeout, ouch'$/) do
puts 'Err, timeout, ouch'
end
我该怎么做?
答案 0 :(得分:1)
Selenium具有隐式等待的概念。也就是说,selenium在声明失败之前重试操作的持续时间。您可以通过在env.rb中设置以下内容来控制隐式等待:
# Set the amount of time the driver should wait when searching for elements
driver.manage.timeouts.implicit_wait = 20
# Sets the amount of time to wait for an asynchronous script to finish
# execution before throwing an error. If the timeout is negative, then the
# script will be allowed to run indefinitely.
driver.manage.timeouts.script_timeout = 20
# Sets the amount of time to wait for a page load to complete before throwing an error.
# If the timeout is negative, page loads can be indefinite.
driver.manage.timeouts.page_load = 20
单位是秒。您需要将implicit_wait设置为高于“Err,timeout,ouch”超时。认为
我相信当等待时间超过时,WebDriver会抛出 Error :: TimeOutError 。你的代码抛出了什么?超时::错误?所以在救援部门:
Given(/^ .... $/ do
...
rescue Error::TimeOutError => e
@timeout_exception = "Error::TimeOutError"
end
rescue Timeout::Error => f
@timeout_exception = "Err, timeout, ouch"
end
end
Then(/^the output should be 'Err, timeout, ouch'$/) do |expectedException|
expect(@timeout_exception).to eq(expectedException), "Err, timeout, ouch"
end
以上假设您使用的是rspec / exceptions,即RSpec 3。
答案 1 :(得分:0)
首先,您需要能够以保证超时的方式执行需要测试的代码。这是调用cli并设置配置文件的唯一选择,但如果你可以直接从步骤定义中调用代码的相关部分,那么你会好得多。
接下来,您希望能够在“然后”步骤中测试“何时”步骤的结果。您将实际调用包含在'try'块中的'When'步骤中,并将结果以及任何异常存储在'Then'步骤将能够对它们执行断言的地方。
有些事情(请原谅语法):
Given(/a configuration that will cause a timeout/) do
sharedContext.config = createConfigThatWillCauseTimeout()
When(/a command is executed/) do
begin:
sharedContext.result = executeCommand(sharedContext.config)
rescue Exception => ex
sharedContext.exception = ex
Then(/there should have been a Timeout/) do
logContent = loadLogContent(sharedContext.config)
assert sharedContext.exception.is_a?(Timeout::Error)
assert logContent.contains("Err, timeout, ouch")