Class: Watir::ElementCollection

Inherits:
Object
  • Object
show all
Includes:
Enumerable, Exception, JSSnippets, Locators::ClassHelpers
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 JSSnippets

#execute_js

Constructor Details

#initialize(query_scope, selector) ⇒ ElementCollection

Returns a new instance of ElementCollection.


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

def initialize(query_scope, selector)
  @query_scope = query_scope
  @selector = selector

  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

142
143
144
# File 'lib/watir/element_collection.rb', line 142

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:


56
57
58
59
60
61
62
63
64
65
66
# File 'lib/watir/element_collection.rb', line 56

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:


126
127
128
# File 'lib/watir/element_collection.rb', line 126

def browser
  @query_scope.browser
end

#buildObject


40
41
42
# File 'lib/watir/element_collection.rb', line 40

def build
  selector_builder.build(@selector.dup)
end

#each {|element| ... } ⇒ Object

Yields each element in 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.


31
32
33
# File 'lib/watir/element_collection.rb', line 31

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

#firstWatir::Element

First element of the collection

Returns:

  • (Watir::Element)

    Returns an instance of a Watir::Element subclass


74
75
76
# File 'lib/watir/element_collection.rb', line 74

def first
  self[0]
end

#lastWatir::Element

Last element of the collection

Returns:

  • (Watir::Element)

    Returns an instance of a Watir::Element subclass


84
85
86
# File 'lib/watir/element_collection.rb', line 84

def last
  self[-1]
end

#locateObject

Locate all elements and return self.

Returns:

  • ElementCollection


115
116
117
118
# File 'lib/watir/element_collection.rb', line 115

def locate
  to_a
  self
end

#to_aArray<Watir::Element>

This collection as an Array.

Returns:


94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/watir/element_collection.rb', line 94

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