Class: Watir::ElementCollection

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/watir-webdriver/element_collection.rb

Overview

Base class for element collections.

Instance Method Summary collapse

Constructor Details

#initialize(parent, selector) ⇒ ElementCollection

Returns a new instance of ElementCollection.



11
12
13
14
# File 'lib/watir-webdriver/element_collection.rb', line 11

def initialize(parent, selector)
  @parent   = parent
  @selector = selector
end

Instance Method Details

#[](idx) ⇒ Object



45
46
47
# File 'lib/watir-webdriver/element_collection.rb', line 45

def [](idx)
  to_a[idx] || element_class.new(@parent, :index => idx)
end

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

Yield Parameters:

  • element (Watir::Element)

    Iterate through the elements in this collection.



20
21
22
# File 'lib/watir-webdriver/element_collection.rb', line 20

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

#firstWatir::Element

First element of this collection

Returns:

  • (Watir::Element)

    Returns an instance of a Watir::Element subclass



55
56
57
# File 'lib/watir-webdriver/element_collection.rb', line 55

def first
  self[0]
end

#lastWatir::Element

Last element of the collection

Returns:

  • (Watir::Element)

    Returns an instance of a Watir::Element subclass



65
66
67
# File 'lib/watir-webdriver/element_collection.rb', line 65

def last
  self[-1]
end

#lengthFixnum Also known as: size

Returns The number of elements in this collection.

Returns:

  • (Fixnum)

    The number of elements in this collection.



28
29
30
# File 'lib/watir-webdriver/element_collection.rb', line 28

def length
  elements.length
end

#to_aArray<Watir::Element>

This collection as an Array

Returns:



75
76
77
78
# File 'lib/watir-webdriver/element_collection.rb', line 75

def to_a
  # TODO: optimize - lazy element_class instance?
  @to_a ||= elements.map { |e| element_class.new(@parent, :element => e) }
end