Class: Watir::ElementCollection

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/watir-classic/element_collection.rb

Overview

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

Instance Method Summary collapse

Constructor Details

#initialize(container, specifiers) ⇒ ElementCollection

Super class for all the iterator classes

* container - an instance of an IE object


9
10
11
12
13
14
15
16
17
18
# File 'lib/watir-classic/element_collection.rb', line 9

def initialize(container, specifiers)
  if specifiers[:index]
    raise Exception::MissingWayOfFindingObjectException,
                "#{self.class} does not support attribute :index in #{specifiers.inspect}"
  end

  @container = container
  @specifiers = specifiers
  @page_container = container.page_container
end

Instance Method Details

#[](n) ⇒ Object

allows access to a specific item in the collection



34
35
36
37
38
39
40
# File 'lib/watir-classic/element_collection.rb', line 34

def [](n)
  number = n - Watir::IE.base_index
  offset = Watir::IE.zero_based_indexing ? (length - 1) : length
  non_existing_element = element_class.new(@container, @specifiers.merge(:index => n))
  def non_existing_element.locate; nil end
  iterator_object(number) || non_existing_element
end

#eachObject

iterate through each of the elements in the collection in turn



29
30
31
# File 'lib/watir-classic/element_collection.rb', line 29

def each
  @container.locator_for(TaggedElementLocator, @specifiers, element_class).each {|element| yield element}
end

#firstObject



42
43
44
# File 'lib/watir-classic/element_collection.rb', line 42

def first
  iterator_object(0)
end

#inspectObject



54
55
56
# File 'lib/watir-classic/element_collection.rb', line 54

def inspect
  '#<%s:0x%x length=%s container=%s>' % [self.class, hash*2, length.inspect, @container.inspect]
end

#lastObject



46
47
48
# File 'lib/watir-classic/element_collection.rb', line 46

def last
  iterator_object(length - 1)
end

#lengthObject Also known as: size



20
21
22
23
24
# File 'lib/watir-classic/element_collection.rb', line 20

def length
  count = 0
  each {|element| count += 1 }
  count
end

#to_sObject



50
51
52
# File 'lib/watir-classic/element_collection.rb', line 50

def to_s
  map { |e| e.to_s }.join("\n")
end