I am using the watir-webdriver ruby gem. It starts the browser (Chrome) and begins to load a page. The page is loading too slowly, and watir-webdriver raises a timeout error. How can I make the browser stop loading the page?
require 'watir-webdriver'
client = Selenium::WebDriver::Remote::Http::Default.new
client.timeout = 10
@browser = Watir::Browser.new :chrome, :http_client => client
sites = [
"http://google.com/",
"http://yahoo.com/",
"http://www.nst.com.my/", # => This is the SLOW site
"http://drupal.org/",
"http://www.msn.com/",
"http://stackoverflow.com/"
]
sites.each do |url|
begin
@browser.goto(url)
puts "Success #{url}"
rescue
puts "Timeout #{url}"
end
end
########## Execution result ##########
# Success http://google.com/
# Success http://yahoo.com/
# Timeout http://www.nst.com.my/
# Timeout http://drupal.org/
# Timeout http://www.msn.com/
# Timeout http://stackoverflow.com/
########## Expected result ##########
# Success http://google.com/
# Success http://yahoo.com/
# Timeout http://www.nst.com.my/
# Success http://drupal.org/
# Success http://www.msn.com/
# Success http://stackoverflow.com/
It looks like the browser doesn't respond to any other commands before it finishes loading the page. How can I force the browser to throw away the page it is loading and execute the next command?
UPDATED
I have found an interesting capability flag loadAsync http://src.chromium.org/svn/trunk/src/chrome/test/webdriver/webdriver_capabilities_parser.cc Maybe it can be useful for solving this problem? I don't understand yet how to make watir (webdriver) to set this when starting the chromedriver. This flag was introduced here http://codereview.chromium.org/7582005/
You can stop Google loading the page using AutoIT to send the Escape key. This is similar to what you originally tried to do, but using AutoIT directly rather than through the broken Watir::Browser object.
Note: I tried to get
autoit.ControlSend("Google", "", "", "{ESC}")
to work so that it would not need the browser to be the active window. While it worked when run by itself, for some reason I could never get it to work in the above script (ie key was sent but browser did not react as expected).There are a couple of different ways to do what you're wanting, but here's what I would do:
If you have a lot of sites you're trying to load for confirmation of timeout or not, put them in an array:
UPDATE for proof of concept. This script always proceeds to step 2. The rescue isn't even necessary for the second goto, but is being used for clearer output. How is your script different than this?
I've been fighting with this problem for awhile, I know that this post is from 2012 but I still haven't found anything that solves this issue.. So i made a work around.
The extra code that saves and restores your cookies will allow you to stay logged into sites you're using. This sucks but its the only work around I can think of. Again this was back in 2012, so if anyone finds anything that works better please correct me.
I have had the problem of timeouts as well. My understanding from online research is that after Selenium WebDriver encounters a timeout, it gets into bad shape and misbehaves (multithreading issue). I have followed the advices found here:
https://github.com/watir/watir-webdriver/issues/137
I implemented active killing (instead of browser.close) and restarting of the
Watir::Browser
after any exception rescue.