preload
Jan 04

One of my customers has a web proxy that handles things like timeout and bad DNS lookups. The problem I run into is that their proxy server does not pass back any HTTP error codes, only a very brief HTML page with the error. This precludes using the built-in HTTP error checking code (check_for_http_error)in Watir that I have used before.   Not wanting to rewrite a bunch of code, I just extended the Watir class to include a custom error check method:

require 'watir'
require 'timeout'
...
class Watir::IE
  def company_error?
    g = self.html[/\<BIG\>(Network Error .*)\<\/BIG\>/, 1]
    return g if g   ## if no error, g = nil
    return false
  end
end
...
url = "http://internal.company.com/page/"
ie = Watir::IE.new
begin
  Timeout::timeout(15) do
    ie.goto url
  end
rescue Exception => e
  # your timeout error code here
  exit
end
if ie.company_error?
  puts "ERROR: #{ie.company_error?}"
  exit
end

The method output looks like this:

irb(main):1683:0> test = "http://beblatt.blar"
=> "http://beblatt.blar"
irb(main):1684:0> ie.goto test
=> 0.658
irb(main):1685:0> ie.company_error?
=> "Network Error (dns_unresolved_hostname)"
irb(main):1686:0>

This catches any timeouts waiting to connect to the web page, and any of the custom proxy errors that could be returned. For ease of making the example code, it just exits on an error, but in the real world, I would take steps to recover or retry.

SociBook del.icio.us Digg Facebook Google Yahoo Buzz StumbleUpon
Tagged with: