Thursday, March 29, 2012

Stop loading browser page

Sometimes the page is loading too slow, and we get a timeout error from watir-webdriver. Like in the below case:

require 'watir-webdriver'
browser = Watir::Browser.new :chrome
begin
  browser.goto("any url for a slow site")
 # Raise Timeout exception in 60 sec
rescue
  browser.send_keys(:escape) # => Raise another Timeout exception
end

Browser just refuses to respond to any othere commands before it finished loading the page. AS in this case the escape command is never executed until page loading is done.

This can be solved in below way:

require 'watir-webdriver'

client = Selenium::WebDriver::Remote::Http::Default.new
client.timeout = 60 
@browser = Watir::Browser.new :chrome, :http_client => client

begin
  @browser.goto mySite
rescue => e
  puts "Browser timed out: #{e}"
end

next_command

I hope this helps...

No comments:

Post a Comment