I have always learned that good coding means: do not repeat yourself. But these days I keep repeating myself, in an attempt to let my scrapers handle timeout errors.
For every link or button that I click, i add a rescue Exception => e and refresh the page.
For example
browser.link(:xpath, "//tr[@class='pager'][1]/td/a").when_present.click
turns into
begin
browser.link(:xpath, "//tr[@class='pager'][1]/td/a").wait_until_present
browser.link(:xpath, "//tr[@class='pager'][1]/td/a").click
rescue Exception => e
sleep (10)
puts "timed out, let's refresh"
browser.refresh
end
Perhaps there is a cleaner, non-repetitive solution. I've tried manipulating the Watir::wait but I could not get that to work..
class Watir::Wait
alias_method :__do_wait, :wait_until
def wait_until
begin
__do_wait{ yield }
rescue Exception => e
puts "timed out. let's refresh"
$browser.refresh
end
end
end
Sorry for my enlish, i'm french.... lol this script allows to refresh browser without to skip a step, for exemple when you have to login, you can't skip a step..... and if you have a website with too many connexion with a low bandwhith.....
I think that seeb would do this :
module Watir
class WhenPresentDecorator
def method_missing(m, *args, &block)
unless @element.respond_to?(m)
raise NoMethodError, "undefined method `#{m}' for #{@element.inspect}:# {@element.class}"
end
begin
while !@element.present?
sleep(10)
puts "timed out, let's refresh"
@element.browser.refresh
end
rescue
else
@element.__send__(m, *args, &block)
end
end
end # WhenPresentDecorator
end