Class: Watir::ElementCollection

Inherits:
Object
  • Object
show all
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

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

#wait_until, #wait_while

Methods included from JSSnippets

#execute_js

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.

Examples:

browser.select_list(name: "new_user_languages").options == browser.select_list(id: "new_user_languages").options
#=> true
browser.select_list(name: "new_user_role").options == browser.select_list(id: "new_user_languages").options
#=> false


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

Parameters:

  • value (Integer, Range)

    Index (0-based) or Range of desired element(s)

Returns:



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

#browserWatir::Browser

Returns browser.

Returns:



134
135
136
# File 'lib/watir/element_collection.rb', line 134

def browser
  @query_scope.browser
end

#buildObject



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.

Examples:

divs = browser.divs(class: 'kls')
divs.each do |div|
  puts div.text
end

Yield Parameters:

  • element (Watir::Element)

    Iterate through the elements in this collection.



35
36
37
38
# File 'lib/watir/element_collection.rb', line 35

def each(&blk)
  reset!
  to_a.each(&blk)
end

#firstWatir::Element

First element of the collection

Returns:

  • (Watir::Element)

    Returns an instance of a Watir::Element subclass



82
83
84
# File 'lib/watir/element_collection.rb', line 82

def first
  self[0]
end

#lastWatir::Element

Last element of the collection

Returns:

  • (Watir::Element)

    Returns an instance of a Watir::Element subclass



92
93
94
# File 'lib/watir/element_collection.rb', line 92

def last
  self[-1]
end

#locateObject

Locate all elements and return self.

Returns:

  • ElementCollection



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.

Examples:

options = browser.select_list(name: "new_user_languages").options
options.reset!
options[0]
#=> nil


165
166
167
# File 'lib/watir/element_collection.rb', line 165

def reset!
  @to_a = nil
end

#to_aArray<Watir::Element>

This collection as an Array.

Returns:



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 ||=
    elements_with_tags.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