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_until_present, #wait_while, #wait_while_present

Methods included from JSSnippets

#execute_js

Constructor Details

#initialize(query_scope, selector) ⇒ ElementCollection

Returns a new instance of ElementCollection.


13
14
15
16
17
18
19
# File 'lib/watir/element_collection.rb', line 13

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

148
149
150
# File 'lib/watir/element_collection.rb', line 148

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:


62
63
64
65
66
67
68
69
70
71
72
# File 'lib/watir/element_collection.rb', line 62

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:


132
133
134
# File 'lib/watir/element_collection.rb', line 132

def browser
  @query_scope.browser
end

#buildObject


46
47
48
# File 'lib/watir/element_collection.rb', line 46

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.


33
34
35
36
# File 'lib/watir/element_collection.rb', line 33

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


80
81
82
# File 'lib/watir/element_collection.rb', line 80

def first
  self[0]
end

#lastWatir::Element

Last element of the collection

Returns:

  • (Watir::Element)

    Returns an instance of a Watir::Element subclass


90
91
92
# File 'lib/watir/element_collection.rb', line 90

def last
  self[-1]
end

#locateObject

Locate all elements and return self.

Returns:

  • ElementCollection


121
122
123
124
# File 'lib/watir/element_collection.rb', line 121

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

163
164
165
# File 'lib/watir/element_collection.rb', line 163

def reset!
  @to_a = nil
end

#to_aArray<Watir::Element>

This collection as an Array.

Returns:


100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/watir/element_collection.rb', line 100

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