module Watir class TaggedElementLocator include Watir include Watir::Exception def initialize(container, tag) @container = container @tag = tag end def set_specifier(how, what) if how.class == Hash and what.nil? specifiers = how else specifiers = {how => what} end @specifiers = {:index => 1} # default if not specified specifiers.each do |how, what| what = what.to_i if how == :index how = :href if how == :url how = :class_name if how == :class @specifiers[how] = what end end def each_element tag @container.document.getElementsByTagName(tag).each do |ole_element| yield Element.new(ole_element) end end def locate index_target = @specifiers[:index] count = 0 each_element(@tag) do |element| catch :next_element do @specifiers.each do |how, what| next if how == :index unless match? element, how, what throw :next_element end end count += 1 unless index_target == count throw :next_element end return element.ole_object end end # elements nil end def match?(element, how, what) begin method = element.method(how) rescue NameError raise MissingWayOfFindingObjectException, "#{how} is an unknown way of finding a <#{@tag}> element (#{what})" end case method.arity when 0 what.matches method.call when 1 method.call(what) else raise MissingWayOfFindingObjectException, "#{how} is an unknown way of finding a <#{@tag}> element (#{what})" end end end end