Class: Celerity::ElementCollection
- Inherits:
-
Object
- Object
- Celerity::ElementCollection
- Includes:
- Enumerable
- Defined in:
- lib/celerity/element_collection.rb
Overview
This class is the superclass for the iterator classes (Buttons, Links, Spans etc.) It would normally only be accessed by the iterator methods (Browser#spans, Browser#links, …).
Direct Known Subclasses
Areas, Buttons, CheckBoxes, Dds, Dels, Divs, Dls, Dts, Ems, FileFields, Forms, Frames, H1s, H2s, H3s, H4s, H5s, H6s, Hiddens, Images, Inses, Labels, Links, Lis, Maps, Metas, Ols, Options, Pres, Ps, Radios, SelectLists, Spans, Strongs, TableBodies, TableCells, TableFooters, TableHeaders, TableRows, Tables, TextFields, Uls
Instance Method Summary collapse
-
#[](n) ⇒ Celerity::Element
Get the element at the given index.
- #each {|element| ... } ⇒ Object
-
#empty? ⇒ Boolean
Returns true if the collection is empty.
-
#first ⇒ Celerity::Element
Get the first element in this collection.
-
#initialize(container, how = nil, what = nil) ⇒ ElementCollection
constructor
private
A new instance of ElementCollection.
-
#last ⇒ Celerity::Element
Get the last element in this collection.
-
#length ⇒ Fixnum
(also: #size)
The number of elements in this collection.
-
#to_s ⇒ String
Note: This can be quite useful in irb:.
Constructor Details
#initialize(container, how = nil, what = nil) ⇒ ElementCollection
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a new instance of ElementCollection.
15 16 17 18 19 |
# File 'lib/celerity/element_collection.rb', line 15 def initialize(container, how = nil, what = nil) @container = container @object = (how == :object ? what : nil) @length = length end |
Instance Method Details
#[](n) ⇒ Celerity::Element
Get the element at the given index. By default, this is 1-indexed to keep compatibility with Watir.
Also note that because of Watir’s lazy loading, this will return an Element instance even if the index is out of bounds.
68 69 70 71 72 73 74 |
# File 'lib/celerity/element_collection.rb', line 68 def [](n) if @elements && @elements[n - Celerity.index_offset] element_class.new(@container, :object, @elements[n - Celerity.index_offset]) else iterator_object(n - Celerity.index_offset) end end |
#each {|element| ... } ⇒ Object
47 48 49 50 51 52 53 54 55 |
# File 'lib/celerity/element_collection.rb', line 47 def each if @elements @elements.each { |e| yield(element_class.new(@container, :object, e)) } else 0.upto(@length - 1) { |i| yield iterator_object(i) } end @length end |
#empty? ⇒ Boolean
Returns true if the collection is empty.
39 40 41 |
# File 'lib/celerity/element_collection.rb', line 39 def empty? length == 0 end |
#first ⇒ Celerity::Element
Get the first element in this collection. (Celerity-specific)
82 83 84 |
# File 'lib/celerity/element_collection.rb', line 82 def first self[Celerity.index_offset] end |
#last ⇒ Celerity::Element
Get the last element in this collection. (Celerity-specific)
92 93 94 |
# File 'lib/celerity/element_collection.rb', line 92 def last self[Celerity.index_offset - 1] end |
#length ⇒ Fixnum Also known as: size
Returns The number of elements in this collection.
25 26 27 28 29 30 31 32 |
# File 'lib/celerity/element_collection.rb', line 25 def length if @object @object.length else @elements ||= ElementLocator.new(@container, element_class).elements_by_idents @elements.size end end |
#to_s ⇒ String
Note: This can be quite useful in irb:
puts browser.text_fields
104 105 106 |
# File 'lib/celerity/element_collection.rb', line 104 def to_s map { |e| e.to_s }.join("\n") end |