Skip to content

Instantly share code, notes, and snippets.

@mscottford
Created January 7, 2010 13:43
Show Gist options
  • Save mscottford/271231 to your computer and use it in GitHub Desktop.
Save mscottford/271231 to your computer and use it in GitHub Desktop.
Dynamically adds "wait_to_" variants for common watir methods. Calling these variants will force the browser to first wait for the browser to finish, and then to wait for the specified element to exist before performing the desired action.
require 'watir'
require 'watir/container'
require 'watir/element'
require 'watir/input_elements'
require 'watir/link'
def support_wait_to_methods
timeout_in_seconds = 30
original_method_missing = self.instance_method(:method_missing)
define_method(:method_missing) do |symbol, *args|
name = symbol.id2name
if name =~ /^wait_to_/
new_symbol = name.gsub("wait_to_", "").to_sym
self.wait
Watir::Waiter.wait_until(timeout_in_seconds) do
exists_and_contains_option_if_select?(new_symbol, args)
end
self.send(new_symbol, *args)
else
original_method_missing.bind(self).call(symbol, args)
end
end
def exists_and_contains_option_if_select?(symbol, args)
result = false
if self.exist?
result = true
# I really don't like sticking this here. It violates my
# desgin sense, but I am going to leave it here for now
# because I am lazy. This will get reworked if I roll this
# into a proper patch.
if symbol == :select
self.locate
result = self.include?(args[0])
end
end
result
end
end
class Watir::InputElement
support_wait_to_methods
end
class Watir::Link
support_wait_to_methods
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment