lib/watir/locator.rb in watir-1.8.0 vs lib/watir/locator.rb in watir-1.8.1.rc1

- old
+ new

@@ -12,17 +12,25 @@ how = :href when :class how = :class_name when :caption how = :value + when :method + how = :form_method end @specifiers[how] = what end end - + def match_with_specifiers?(element) + @specifiers.each do |how, what| + next if how == :index + return false unless match? element, how, what + end + return true + end end class TaggedElementLocator < Locator def initialize(container, tag) @container = container @@ -47,48 +55,74 @@ end def locate 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 count == @specifiers[:index] - throw :next_element - end - return element.ole_object - end - + next unless match_with_specifiers?(element) + count += 1 + return element.ole_object if count == @specifiers[:index] 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})" + "#{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})" + "#{how} is an unknown way of finding a <#{@tag}> element (#{what})" end end end + + class FrameLocator < TaggedElementLocator + attr_accessor :tag + + def initialize(container) + @container = container + end + + def each_element tag + frames = @container.document.frames + i = 0 + @container.document.getElementsByTagName(tag).each do |frame| + element = Element.new(frame) + document = frames.item(i) + yield element, document + i += 1 + end + end + + def locate + count = 0 + each_element(@tag) do |element, document| + next unless match_with_specifiers?(element) + count += 1 + return element.ole_object, document if count == @specifiers[:index] + end # elements + nil + end + end + + class FormLocator < TaggedElementLocator + def each_element(tag) + @container.document.forms.each do |form| + yield FormElement.new(form) + end + end + end + class InputElementLocator < Locator attr_accessor :document, :element, :elements, :klass def initialize container, types @@ -124,23 +158,14 @@ element = @klass.new(@container, @specifiers, nil) element.ole_object = object def element.locate; @o; end end - catch :next_element do - throw :next_element unless @types.include?(element.type) - @specifiers.each do |how, what| - next if how == :index - unless match? element, how, what - throw :next_element - end - end - count += 1 - throw :next_element unless count == @specifiers[:index] - return object - end - + next unless @types.include?(element.type) && match_with_specifiers?(element) + + count += 1 + return object if count == @specifiers[:index] end nil end # return true if the element matches the provided how and what def match? element, how, what @@ -164,11 +189,11 @@ # getElementById() if the matching element actually HAS a matching # :id. the_id = @specifiers[:id] if the_id && the_id.class == String && - @specifiers[:index] == 1 && @specifiers.length == 2 + @specifiers[:index] == 1 && @specifiers.length == 2 @element = @document.getElementById(the_id) rescue nil # Return if our fast match really HAS a matching :id return true if @element && @element.invoke('id') == the_id end @@ -188,18 +213,11 @@ end def each count = 0 each_element('*') 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 - yield element - end + next unless match_with_specifiers?(element) + yield element end nil end end