Class: Watir::ElementCollection
- Inherits:
-
Object
- Object
- Watir::ElementCollection
- 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 Container.
Direct Known Subclasses
HTMLElementCollection, InputElementCollection, TableElementCollection
Instance Method Summary collapse
-
#[](n) ⇒ Element
Access a specific item in the collection.
-
#each {|element| ... } ⇒ Object
Iterate through each of the elements in the collection in turn.
-
#first ⇒ Element
First element from this collection.
-
#initialize(container, specifiers) ⇒ ElementCollection
constructor
Super class for all the iterator classes.
- #inspect ⇒ Object
-
#last ⇒ Element
Last element from this collection.
-
#length ⇒ Fixnum
(also: #size)
Count of elements in this collection.
-
#to_s ⇒ String
String representation of each element in this collection separated by line-feed.
Constructor Details
#initialize(container, specifiers) ⇒ ElementCollection
Super class for all the iterator classes
10 11 12 13 14 15 16 17 18 19 |
# File 'lib/watir-classic/element_collection.rb', line 10 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) ⇒ Element
Watir::Element will be always returned even if the index is out of bounds. Use Watir::Element#exists? to verify if the element actually exists.
Access a specific item in the collection.
42 43 44 45 46 |
# File 'lib/watir-classic/element_collection.rb', line 42 def [](n) non_existing_element = element_class.new(@container, @specifiers.merge(:index => n)) def non_existing_element.locate; nil end iterator_object(n) || non_existing_element end |
#each {|element| ... } ⇒ Object
Iterate through each of the elements in the collection in turn.
32 33 34 |
# File 'lib/watir-classic/element_collection.rb', line 32 def each @container.locator_for(TaggedElementLocator, @specifiers, element_class).each {|element| yield element} end |
#first ⇒ Element
Returns first element from this collection.
49 50 51 |
# File 'lib/watir-classic/element_collection.rb', line 49 def first iterator_object(0) end |
#inspect ⇒ Object
64 65 66 |
# File 'lib/watir-classic/element_collection.rb', line 64 def inspect '#<%s:0x%x length=%s container=%s>' % [self.class, hash*2, length.inspect, @container.inspect] end |
#last ⇒ Element
Returns last element from this collection.
54 55 56 |
# File 'lib/watir-classic/element_collection.rb', line 54 def last iterator_object(length - 1) end |
#length ⇒ Fixnum Also known as: size
Returns count of elements in this collection.
22 23 24 25 26 |
# File 'lib/watir-classic/element_collection.rb', line 22 def length count = 0 each {|element| count += 1 } count end |
#to_s ⇒ String
Returns String representation of each element in this collection separated by line-feed.
60 61 62 |
# File 'lib/watir-classic/element_collection.rb', line 60 def to_s map { |e| e.to_s }.join("\n") end |