Watir:通过启动chrome浏览器有时会出现Net :: ReadTimeout错误

时间:2017-11-23 09:46:36

标签: ruby watir watir-webdriver

我只是使用watir脚本从服务器下载顺序状态信息。在大多数情况下它工作正常,但有时我得到一个Net :: ReadTimeout错误。我不知道为什么?

代码:

require "rubygems"
require "watir"
Watir.default_timeout = 180     # seconds – default is 60
prefs = { :download => { :prompt_for_download => false, :default_directory => path } }
browser = Watir::Browser.new :chrome, :switches => %w[--no-sandbox], :prefs => prefs
browser.goto 'https://www...'
...

错误讯息:

/usr/lib/ruby/2.3.0/net/protocol.rb:158:in `rbuf_fill': Net::ReadTimeout (Net::ReadTimeout)   
    from /usr/lib/ruby/2.3.0/net/protocol.rb:136:in `readuntil' 
    from /usr/lib/ruby/2.3.0/net/protocol.rb:146:in `readline'  
    from /usr/lib/ruby/2.3.0/net/http/response.rb:40:in `read_status_line'
    from /usr/lib/ruby/2.3.0/net/http/response.rb:29:in `read_new'
    from /usr/lib/ruby/2.3.0/net/http.rb:1437:in `block in transport_request'  
    from /usr/lib/ruby/2.3.0/net/http.rb:1434:in `catch'
    from /usr/lib/ruby/2.3.0/net/http.rb:1434:in `transport_request'from /usr/lib/ruby/2.3.0/net/http.rb:1407:in `request'
    from /usr/lib/ruby/2.3.0/net/http.rb:1400:in `block in request'
    from /usr/lib/ruby/2.3.0/net/http.rb:853:in `start' 
    from /usr/lib/ruby/2.3.0/net/http.rb:1398:in `request'
    from /var/lib/gems/2.3.0/gems/selenium-webdriver-3.0.1/lib/selenium/webdriver/remote/http/default.rb:103:in `response_for'
    from /var/lib/gems/2.3.0/gems/selenium-webdriver-3.0.1/lib/selenium/webdriver/remote/http/default.rb:57:in `request' 
    from /var/lib/gems/2.3.0/gems/selenium-webdriver-3.0.1/lib/selenium/webdriver/remote/http/common.rb:59:in `call'   
    from /var/lib/gems/2.3.0/gems/selenium-webdriver-3.0.1/lib/selenium/webdriver/remote/bridge.rb:653:in `raw_execute'
    from /var/lib/gems/2.3.0/gems/selenium-webdriver-3.0.1/lib/selenium/webdriver/remote/bridge.rb:124:in `create_session'
    from /var/lib/gems/2.3.0/gems/selenium-webdriver-3.0.1/lib/selenium/webdriver/remote/bridge.rb:88:in `initialize'  
    from /var/lib/gems/2.3.0/gems/selenium-webdriver-3.0.1/lib/selenium/webdriver/chrome/bridge.rb:41:in `initialize'  
    from /var/lib/gems/2.3.0/gems/selenium-webdriver-3.0.1/lib/selenium/webdriver/common/driver.rb:61:in `new'  
    from /var/lib/gems/2.3.0/gems/selenium-webdriver-3.0.1/lib/selenium/webdriver/common/driver.rb:61:in `for' 
    from /var/lib/gems/2.3.0/gems/selenium-webdriver-3.0.1/lib/selenium/webdriver.rb:82:in `for'
    from /var/lib/gems/2.3.0/gems/watir-6.0.2/lib/watir/browser.rb:46:in `initialize'   
    from /var/www/jobs/ubuntu.rb:110:in `new'from /var/www/jobs/ubuntu.rb:5:in `<main>'

我只是在寻找原因?

是否有可能在Firefox上自定义设置?有什么问题?

  • Watir Version:6.0.2
  • Selenium版本:3.0.1
  • 浏览器Chrome版本:57.0.2987.110
  • Chrome驱动程序版本:2.29.461571
  • 操作系统版本:Ubuntu 16.04.3 LTS

3 个答案:

答案 0 :(得分:3)

在我的Ubuntu Jenkins构建服务器上运行Selenium Webdriver测试时遇到了类似的问题,但我从未在Windows PC上本地体验过这个问题。像你的问题,这是一个间歇性的问题。我花了很多时间研究它并且无法深究它,但我把它归结为Ruby / Ubuntu问题而不是测试问题,因此,我决定拯救网络:: ReadTimeout错误并重试。自从实现以下示例以来,我再没有遇到过这个问题。

     attempts = 0  # has to be outside the begin/rescue to avoid infinite loop
  begin
  profile = Selenium::WebDriver::Chrome::Profile.new
  $driver = Selenium::WebDriver.for :chrome, :profile => profile,
  $driver.manage.window.resize_to(1280, 720)
  rescue Net::ReadTimeout => e
    if attempts == 0
      attempts += 1
      retry
    else
      raise
    end
  end

您应该可以简单地修改上面的示例以使用Watir。它基本上捕获错误并重试/重新运行测试。

答案 1 :(得分:2)

首先,Watir.default_timeout与您所说的错误无关,因此,即使您增加default_timeout的时间,此地点也不会发生任何事情,Watir.default_timeout仅适用于您找到的情况一个元素,而不是page_load。

其次,这个问题只存在于Ruby Selenium Binding中,而不是在Java Selenium Binding中,例如,如果你设置

driver.manage.timeouts.page_load=120 (WATIR doesn't provide any systax to this selenium equivalent, so to write this code in WATIR, please invoke b.driver.)

此时间设置仅适用于goto方法,当您单击按钮并等待页面加载时,它不会影响任何内容,page_load的默认超时为60秒,无论如何等待60秒,但你设置的不会影响或改变这60秒。但是这个问题不适用于Selenium Java Binding,它可以正常工作。

好的,问题是,您是否将它用于goto,那么为什么不使用page_load来设置时间?

实际上你可以减少时间,但是你不能将时间增加到60秒以上。但我找到了解决此问题的一些方法,但我强烈建议您在ruby selenium绑定中提交一个错误。

所以它不是WATIR问题,它是Ruby selenium绑定中的问题。

答案 2 :(得分:1)

最后,我还使用firefox测试了解决方案并获得了所描述的错误:

/var/lib/gems/2.1.0/gems/selenium-webdriver-3.0.1/lib/seleni‌​um/webdriver/remote/‌​response.rb:69:in 'assert_ok': 
TypeError: Given platformVersion [object String] "any", but current platform version is [object String] "3.16.0-4-amd64"
(Selenium::WebDriver::Error::SessionNotCreatedError)

这似乎是特定的firefox和geckodriver版本的问题。我测试了很多组合。此版本在没有TypeError的情况下工作:

  • Firefox 51.0.1
  • geckodriver 0.11.1

感谢dan.brown