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 heard it right, it is called “headless”), it means a browser without a user interface or “head.” So, when the browser is headless, the GUI is hidden. Therefore, when you use a headless browser to access any website, you can't see anything. However, the program runs in the background.
Request your application in the correct language in "lang=en" Use excludeSwitches to remove the "Chrome is being controlled by automated test software" if needed. Set resolution ("--window-size=1920,1080") or start maximized to set the same resolution you use on normal Chrome.
Headless Browsers are Faster than Real Browsers But you will typically see a 2x to 15x faster performance when using a headless browser. So if performance is critical for you, headless browsers may be a way to go.
Nonetheless, instrumenting Headless Chrome with a framework such as Puppeteer will still leave traces that make it possible to detect it as a non-human user.
There are a couple of different ways to do what you're wanting, but here's what I would do:
require 'watir-webdriver'
client = Selenium::WebDriver::Remote::Http::Default.new
client.timeout = 60
@browser = Watir::Browser.new :firefox, :http_client => client
begin
@browser.goto mySite
rescue => e
puts "Browser timed out: #{e}"
end
next_command
If you have a lot of sites you're trying to load for confirmation of timeout or not, put them in an array:
mySites = [
mySite1,
mySite2,
mySite3
]
mySites.each do |site|
begin
@browser.goto site
rescue
"Puts #{site} failed to load within the time allotted."
end
end
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?
require 'watir-webdriver'
client = Selenium::WebDriver::Remote::Http::Default.new
client.timeout = 1 #VERY low timeout to make most sites fail
@browser = Watir::Browser.new :firefox, :http_client => client
def testing
begin
@browser.goto "http://www.cnn.com"
rescue => e
puts "Browser timed out on the first example"
end
begin
@browser.goto "http://www.foxnews.com"
rescue => e
puts "Browser timed out on the second example"
end
end
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.
require 'watir-webdriver'
require 'win32ole'
client = Selenium::WebDriver::Remote::Http::Default.new
client.timeout = 5
@browser = Watir::Browser.new :chrome, :http_client => client
begin
@browser.goto "http://www.nst.com.my/"
rescue
autoit = WIN32OLE.new("AutoItX3.Control")
autoit.AutoItSetOption("WinTitleMatchMode", 2)
autoit.WinActivate("Google")
autoit.Send("{ESC}")
end
@browser.goto "http://www.google.ca"
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With