Skip to content

Instantly share code, notes, and snippets.

@jubishop
Created August 19, 2009 00:35
Show Gist options
  • Save jubishop/170075 to your computer and use it in GitHub Desktop.
Save jubishop/170075 to your computer and use it in GitHub Desktop.
module SlowWatir
IndestructibleMethods = ['__id__', '__send__']
SleepTime = 0.5
def self.await(tries = 20)
begin
result = yield
if not result or (result.respond_to? :exists? and not result.exists?)
raise Exception.new
end
rescue Exception => e
if (tries -= 1) > 0
sleep SleepTime
retry
else
raise e
end
end
result
end
def self.attach(how, what)
self.await { Watir::Browser.attach(how, what) }
end
class Proxy
public_instance_methods.sort.each { |method|
undef_method method unless IndestructibleMethods.include? method
}
def initialize(target = nil)
@target = target
end
def method_missing(name, *args, &block)
self
end
end
class MaybeProxy < Proxy
def initialize(target, tries = 1)
super(target)
@tries = tries
end
def maybe(tries = @tries)
begin
result = yield
if not result or (result.respond_to? :exists? and not result.exists?)
raise Exception.new
else
result
end
rescue Exception => e
if (tries -= 1) > 0
sleep SleepTime
retry
else
Proxy.new
end
end
end
def method_missing(name, *args, &block)
maybe { @target.send(name, *args, &block) }
end
end
class WaitProxy < Proxy
def method_missing(name, *args, &block)
SlowWatir.await { @target.send(name, *args, &block) }
end
end
module Browser
def maybe(tries = 1)
@slow_maybe ||= SlowWatir::MaybeProxy.new(self, tries)
end
def await
@slow_wait ||= SlowWatir::WaitProxy.new(self)
end
end
end
begin
FireWatir::Firefox.send :include, SlowWatir::Browser
rescue Exception => e; end
begin
Watir::IE.send :include, SlowWatir::Browser
rescue Exception => e; end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment