Class: Watir::ElementCollections

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/watir/element_collections.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

Direct Known Subclasses

Forms, Frames, HTMLElements, InputElementCollections, Inses

Instance Method Summary collapse

Constructor Details

#initialize(container, how, what) ⇒ ElementCollections

Super class for all the iteractor classes

* container - an instance of an IE object


9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/watir/element_collections.rb', line 9

def initialize(container, how, what)
  if how == :index || (how.is_a?(Hash) && how[:index])
    _how = what ? "#{how.inspect}, #{what.inspect}" : "#{how.inspect}"
    raise Exception::MissingWayOfFindingObjectException,
                "#{self.class} does not support attribute :index in #{_how}"
  end

  @container = container
  @how = how
  @what = what
  @length = length
  @page_container = container.page_container
end

Instance Method Details

#[](n) ⇒ Object

allows access to a specific item in the collection



37
38
39
40
41
42
43
# File 'lib/watir/element_collections.rb', line 37

def [](n)
  unless n.between?(0, length - 1)
    raise Exception::MissingWayOfFindingObjectException,
      "Can't find #{element_tag.downcase} with :index #{n} from #{self.class} with size of #{length}"
  end
  return iterator_object(n)
end

#eachObject

iterate through each of the elements in the collection in turn



32
33
34
# File 'lib/watir/element_collections.rb', line 32

def each
  @container.tagged_element_locator(element_tag, @how, @what, element_class).each {|element| yield element}
end

#firstObject



45
46
47
# File 'lib/watir/element_collections.rb', line 45

def first
  iterator_object(0)
end

#inspectObject



57
58
59
# File 'lib/watir/element_collections.rb', line 57

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

#lastObject



49
50
51
# File 'lib/watir/element_collections.rb', line 49

def last
  iterator_object(length - 1)
end

#lengthObject Also known as: size



23
24
25
26
27
# File 'lib/watir/element_collections.rb', line 23

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

#to_sObject



53
54
55
# File 'lib/watir/element_collections.rb', line 53

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