2012-12-24 44 views
10

我有一个用于测试UI功能的Cucumber场景。有时由于几个问题之一,网页花了很多时间来回应,水豚超时出现以下错误。超时重试?

ruby-1.9.3-p327/lib/ruby/1.9.1/net/protocol.rb:146:in `rescue in rbuf_fill' 
ruby-1.9.3-p327/lib/ruby/1.9.1/net/protocol.rb:140:in `rbuf_fill' 
ruby-1.9.3-p327/lib/ruby/1.9.1/net/protocol.rb:122:in `readuntil' 
ruby-1.9.3-p327/lib/ruby/1.9.1/net/protocol.rb:132:in `readline' 
ruby-1.9.3-p327/lib/ruby/1.9.1/net/http.rb:2562:in `read_status_line' 
ruby-1.9.3-p327/lib/ruby/1.9.1/net/http.rb:2551:in `read_new' 

我的问题是 -

我可以以某种方式迫使黄瓜方案或Capybara分别重试(恒定的次数),整个场景或步,在超时错误?

回答

2

也许,你可以做这样的:

Around do |scenario, block| 
    for i in 1..5 
    begin 
     block.call 
     break 
    rescue Timeout::Error 
     next 
    end 
    end 
end 

但我想不通,如果这个代码工作,因为错误(It's not possible to call block several times in Around hook

+0

感谢您的答复。这篇文章的第二个答案适用于我,但我会牢记这一点。 –

+0

+1我换掉了Timeout :: Error for just Exception。不知道为什么Timeout :: Error不会为我捕捉它。< – kikuchiyo

+0

出于某种原因,使用Timeout :: Error时,它仍未捕获。放入放置语句,它只运行一次,并失败一次......哦痛苦运行水豚/硒/ webkit在这个应用程序的痛苦! – kikuchiyo

1

The Cucumber book的:

添加一个eventually method不断试图运行一段代码,直到它停止提高错误或达到时间限制。

下面是该方法的代码:

module AsyncSupport 
    def eventually 
    timeout = 2 
    polling_interval = 0.1 
    time_limit = Time.now + timeout 
    loop do 
     begin 
     yield 
     rescue Exception => error 
     end 
     return if error.nil? 
     raise error if Time.now >= time_limit sleep polling_interval 
    end 
    end 
end 
World(AsyncSupport) 

称为被称为从step_definition如下的方法:

Then /^the balance of my account should be (#{CAPTURE_CASH_AMOUNT})$/ do |amount| 
    eventually { my_account.balance.should eq(amount) } 
end 
+0

谢谢!这可能解决了我的问题。不再有错误。 –

+0

很高兴有帮助!如果答案已经帮助你,请将答案标记为已接受。 –