Class: Watir::ElementCollection
- Inherits:
-
Object
- Object
- Watir::ElementCollection
- Includes:
- Enumerable, Exception, JSSnippets, Locators::ClassHelpers, Waitable
- Defined in:
- lib/watir/element_collection.rb
Overview
Base class for element collections.
Direct Known Subclasses
AnchorCollection, AppletCollection, AreaCollection, AudioCollection, BRCollection, BaseCollection, BodyCollection, ButtonCollection, CanvasCollection, CircleCollection, DListCollection, DataCollection, DataListCollection, DefsCollection, DescCollection, DetailsCollection, DialogCollection, DirectoryCollection, DivCollection, EllipseCollection, EmbedCollection, FieldSetCollection, FontCollection, ForeignObjectCollection, FormCollection, FrameSetCollection, GCollection, GeometryCollection, GradientCollection, GraphicsCollection, HRCollection, HTMLElementCollection, HeadCollection, HeadingCollection, HtmlCollection, IFrameCollection, ImageCollection, InputCollection, LICollection, LabelCollection, LegendCollection, LineCollection, LinearGradientCollection, MapCollection, MarkerCollection, MarqueeCollection, MediaCollection, MetaCollection, MetadataCollection, MeterCollection, ModCollection, OListCollection, ObjectCollection, OptGroupCollection, OptionCollection, OutputCollection, ParagraphCollection, ParamCollection, PathCollection, PatternCollection, PictureCollection, PolygonCollection, PolylineCollection, PreCollection, ProgressCollection, QuoteCollection, RadialGradientCollection, RectCollection, SVGCollection, SVGElementCollection, ScriptCollection, SelectCollection, SourceCollection, SpanCollection, StopCollection, StyleCollection, SwitchCollection, SymbolCollection, TSpanCollection, TableCaptionCollection, TableCellCollection, TableColCollection, TableCollection, TableDataCellCollection, TableHeaderCellCollection, TableRowCollection, TableSectionCollection, TemplateCollection, TextAreaCollection, TextContentCollection, TextPathCollection, TextPositioningCollection, TimeCollection, TitleCollection, TrackCollection, UListCollection, UnknownCollection, UseCollection, VideoCollection, ViewCollection
Instance Method Summary collapse
-
#==(other) ⇒ Object
(also: #eql?)
Returns true if two element collections are equal.
-
#[](value) ⇒ Watir::Element, Watir::ElementCollection
Get the element at the given index or range.
-
#browser ⇒ Watir::Browser
Returns browser.
- #build ⇒ Object
-
#each {|element| ... } ⇒ Object
Relocates elements then yields each element in resulting collection.
-
#first ⇒ Watir::Element
First element of the collection.
-
#initialize(query_scope, selector) ⇒ ElementCollection
constructor
A new instance of ElementCollection.
-
#last ⇒ Watir::Element
Last element of the collection.
-
#locate ⇒ Object
Locate all elements and return self.
-
#reset! ⇒ Object
Removes cache of previously located elements in the collection.
-
#to_a ⇒ Array<Watir::Element>
This collection as an Array.
Methods included from Locators::ClassHelpers
#class_from_string, #element_class_name, #element_matcher_class, #locator, #locator_class, #selector_builder, #selector_builder_class
Methods included from Waitable
Methods included from JSSnippets
Constructor Details
#initialize(query_scope, selector) ⇒ ElementCollection
Returns a new instance of ElementCollection.
15 16 17 18 19 20 21 |
# File 'lib/watir/element_collection.rb', line 15 def initialize(query_scope, selector) @query_scope = query_scope @selector = selector @to_a = nil build unless @selector.key?(:element) end |
Instance Method Details
#==(other) ⇒ Object Also known as: eql?
Returns true if two element collections are equal.
150 151 152 |
# File 'lib/watir/element_collection.rb', line 150 def ==(other) to_a == other.to_a end |
#[](value) ⇒ Watir::Element, Watir::ElementCollection
Get the element at the given index or range.
Any call to an ElementCollection that includes an adjacent selector can not be lazy loaded because it must store the correct type
Ranges can not be lazy loaded
64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/watir/element_collection.rb', line 64 def [](value) if value.is_a?(Range) to_a[value] elsif @selector.key? :adjacent to_a[value] || element_class.new(@query_scope, invalid_locator: true) elsif @to_a && @to_a[value] @to_a[value] else element_class.new(@query_scope, @selector.merge(index: value)) end end |
#browser ⇒ Watir::Browser
Returns browser.
134 135 136 |
# File 'lib/watir/element_collection.rb', line 134 def browser @query_scope.browser end |
#build ⇒ Object
48 49 50 |
# File 'lib/watir/element_collection.rb', line 48 def build selector_builder.build(@selector.dup) end |
#each {|element| ... } ⇒ Object
Relocates elements then yields each element in resulting collection.
35 36 37 38 |
# File 'lib/watir/element_collection.rb', line 35 def each(&blk) reset! to_a.each(&blk) end |
#first ⇒ Watir::Element
First element of the collection
82 83 84 |
# File 'lib/watir/element_collection.rb', line 82 def first self[0] end |
#last ⇒ Watir::Element
Last element of the collection
92 93 94 |
# File 'lib/watir/element_collection.rb', line 92 def last self[-1] end |
#locate ⇒ Object
Locate all elements and return self.
123 124 125 126 |
# File 'lib/watir/element_collection.rb', line 123 def locate to_a self end |
#reset! ⇒ Object
Removes cache of previously located elements in the collection.
165 166 167 |
# File 'lib/watir/element_collection.rb', line 165 def reset! @to_a = nil end |
#to_a ⇒ Array<Watir::Element>
This collection as an Array.
102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/watir/element_collection.rb', line 102 def to_a hash = {} @to_a ||= .map.with_index do |(el, tag_name), idx| selector = @selector.dup selector[:index] = idx unless idx.zero? element = element_class.new(@query_scope, selector) if [HTMLElement, Input].include? element.class construct_subtype(element, hash, tag_name).tap { |e| e.cache = el } else element.tap { |e| e.cache = el } end end end |