我知道在Watir中有几个与实现等待和超时有关的问题,但是我还没有找到对我的问题的答案(这一定是常见的)。我使用Watir-webdriver来测试由于AJAX实现长时间(超过5分钟)而逐个加载的页面。我需要能够在有限的时间内(20-40秒)对此页面进行采样,并且能够分析在此短时间内加载的信息。然而,据我所知,没有直接的机制可以告诉Watir :: Browser停止。我可以使用超时,但尽管我的脚本在救援后获得了控制权,但不可能询问浏览器并验证在超时窗口期间能够接收的信息。我现在所能做的就是杀死进程并重新启动浏览器,如下所述:Make headless browser stop loading page和其他地方。使Watir-webdriver在有限的时间内加载一个页面,并能够稍后检索信息
下面的代码说明了我的情况。在这个例子中,我有一个全局超时(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.'