【问题标题】:Retry on timeout?超时重试?
【发布时间】:2012-12-10 18:17:55
【问题描述】:

我有一个用于测试 UI 功能的 Cucumber 场景。有时由于几个问题之一,网页需要很长时间才能响应,Capybara 会超时并出现以下错误。

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' 

我的问题是-

我能否以某种方式强制 Cucumber 场景或 Capybara 在超时错误时分别重试(恒定次数)整个场景或步骤?

【问题讨论】:

    标签: selenium cucumber capybara


    【解决方案1】:

    也许,你可以这样做:

    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) 而工作

    【讨论】:

    • 感谢您的回复。这篇文章的第二个答案对我有用,但我会记住这一点。
    • +1 我将 Timeout::Error 换成了 Exception。不知道为什么 Timeout::Error 没有为我捕捉到它>.
    • 由于某种原因,Timeout::Error 仍然没有被捕获。放入 puts 语句,它只运行一次,失败一次......哦,在这个应用程序上运行 capybara / selenium / webkit 的痛苦!
    • 由于某种原因,Timeout::Error 仍然没有被捕获。放入 puts 语句,它只运行一次,失败一次......哦,在这个应用程序上运行 capybara / selenium / webkit 的痛苦!
    【解决方案2】:

    来自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
    

    【讨论】:

    • 谢谢!这可能解决了我的问题。不再出现错误。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-05-28
    • 1970-01-01
    • 2023-01-15
    • 2020-02-12
    • 1970-01-01
    • 2012-09-01
    • 2020-09-22
    相关资源
    最近更新 更多