使Watir-webdriver在有限的时间内加载页面,以便以后能够检索信息

时间:2015-03-31 19:15:18

标签: watir watir-webdriver

我知道在Watir中有几个与等待和超时的实现相关的问题,但是我没有找到问题的答案(必须是常见的)。我使用Watir-webdriver来测试由于AJAX实现很长时间(超过5分钟)逐个加载的页面。我需要能够在有限的时间(20-40秒)内对此页面进行采样,并能够分析在此短时间内加载的信息。但是,据我所知,没有简单的直接机制来告诉Watir :: Browser停止。我可以使用超时,但是虽然我的脚本在救援后获得控制权,但是无法询问浏览器并验证它在超时窗口期间能够接收的信息。我现在所能做的就是杀死进程并重新启动浏览器,如下所述:Make headless browser stop loading page和其他地方。

下面的代码说明了我的情况。在此示例中,我有一个全局超时(30秒)和一个用于读取页面的本地超时(15秒)。它永远不会被b.text召唤;该脚本只在15秒后输出第一个异常,然后它一直等待浏览器被释放,并在全局超时30秒后打印第二个异常消息。

Time out. Got into exception branch 
Dropped to bottom rescue. 
The end.

我还试图发送一个'逃脱'浏览器的关键,但在goto方法中与它进行任何通信都是不可能的。任何提示和建议将不胜感激!

require 'watir-webdriver'
require 'timeout'

client = Selenium::WebDriver::Remote::Http::Default.new
client.timeout = 30                      # Set the global timeout 

b = Watir::Browser.new :chrome, :http_client => client
my_url = '...here is my address...'

begin
  begin
   Timeout::timeout(15) { b.goto my_url } # Access the page with local timeout
   b.close # if all is unbelievably good and the page is loaded

  rescue Exception => e
    puts 'Time out. Got into exception branch'

    if b.text.include? 'my_text'   # NEVER GETS HERE
      puts 'Yes, I see the text!'
    else
      puts 'I do not see the text.'
    end
  end
rescue Exception => e
 puts 'Dropped to bottom rescue.'
end

puts 'The end.'

1 个答案:

答案 0 :(得分:1)

Watir依靠Selenium WebDriver来处理对浏览器的调用。此时所有浏览器都要求当前帧的document.readyState返回"完成"在将控制权交还给你的代码之前。

最近对webdriver规范的更新似乎允许浏览器驱动程序实现不阻塞的页面加载策略,但这不是必需的,目前不支持。

https://w3c.github.io/webdriver/webdriver-spec.html#the-page-load-strategy