lib/watir/element_collections.rb in watir-2.0.2 vs lib/watir/element_collections.rb in watir-2.0.3
- old
+ new
@@ -1,11 +1,11 @@
module Watir
# this class is the super class for the iterator classes (buttons, links, spans etc
# it would normally only be accessed by the iterator methods (spans, links etc) of IE
class ElementCollections
include Enumerable
-
+
# Super class for all the iteractor classes
# * container - an instance of an IE object
def initialize(container, how, what)
if how == :index || (how.is_a?(Hash) && how[:index])
_how = what ? "#{how.inspect}, #{what.inspect}" : "#{how.inspect}"
@@ -17,11 +17,11 @@
@how = how
@what = what
@length = length
@page_container = container.page_container
end
-
+
def length
count = 0
each {|element| count += 1 }
count
end
@@ -30,18 +30,21 @@
# iterate through each of the elements in the collection in turn
def each
@container.tagged_element_locator(element_tag, @how, @what, element_class).each {|element| yield element}
end
-
+
# allows access to a specific item in the collection
def [](n)
- unless n.between?(0, length - 1)
+ number = n - Watir::IE.base_index
+ offset = Watir::IE.zero_based_indexing ? (length - 1) : length
+
+ unless number.between?(0, offset)
raise Exception::MissingWayOfFindingObjectException,
"Can't find #{element_tag.downcase} with :index #{n} from #{self.class} with size of #{length}"
end
- return iterator_object(n)
+ return iterator_object(number)
end
def first
iterator_object(0)
end
@@ -62,10 +65,10 @@
def iterator_object(i)
count = 0
each {|e| return e if count == i; count += 1}
end
-
+
def element_class
Watir.const_get self.class.name.split("::").last.chop
end
def element_tag