Class: Watir::ElementCollection

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

Overview

This class is the super class for the iterator classes (buttons, links, spans etc). It would normally only be accessed by the iterator methods (spans, links etc) of Container.

Instance Method Summary collapse

Constructor Details

#initialize(container, specifiers) ⇒ ElementCollection

Super class for all the iterator classes

Parameters:

  • container (Element)

    container element instance.

  • specifiers (Hash)

    locators for elements.



10
11
12
13
14
15
16
17
18
19
# File 'lib/watir-classic/element_collection.rb', line 10

def initialize(container, specifiers)
  if specifiers[:index]
    raise Exception::MissingWayOfFindingObjectException,
                "#{self.class} does not support attribute :index in #{specifiers.inspect}"
  end

  @container = container
  @specifiers = specifiers
  @page_container = container.page_container
end

Instance Method Details

#[](n) ⇒ Element

Note:

Watir::Element will be always returned even if the index is out of bounds. Use Watir::Element#exists? to verify if the element actually exists.

Access a specific item in the collection.

Parameters:

  • n (Fixnum)

    n-th element to retrieve.

Returns:

  • (Element)

    element with specified index from this collection.



42
43
44
45
46
# File 'lib/watir-classic/element_collection.rb', line 42

def [](n)
  non_existing_element = element_class.new(@container, @specifiers.merge(:index => n))
  def non_existing_element.locate; nil end
  iterator_object(n) || non_existing_element
end

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

Iterate through each of the elements in the collection in turn.

Yield Parameters:

  • element (Element)

    element instance.



32
33
34
# File 'lib/watir-classic/element_collection.rb', line 32

def each
  @container.locator_for(TaggedElementLocator, @specifiers, element_class).each {|element| yield element}
end

#firstElement

Returns first element from this collection.

Returns:

  • (Element)

    first element from this collection.



49
50
51
# File 'lib/watir-classic/element_collection.rb', line 49

def first
  iterator_object(0)
end

#inspectObject



64
65
66
# File 'lib/watir-classic/element_collection.rb', line 64

def inspect
  '#<%s:0x%x length=%s container=%s>' % [self.class, hash*2, length.inspect, @container.inspect]
end

#lastElement

Returns last element from this collection.

Returns:

  • (Element)

    last element from this collection.



54
55
56
# File 'lib/watir-classic/element_collection.rb', line 54

def last
  iterator_object(length - 1)
end

#lengthFixnum Also known as: size

Returns count of elements in this collection.

Returns:

  • (Fixnum)

    count of elements in this collection.



22
23
24
25
26
# File 'lib/watir-classic/element_collection.rb', line 22

def length
  count = 0
  each {|element| count += 1 }
  count
end

#to_sString

Returns String representation of each element in this collection separated by line-feed.

Returns:

  • (String)

    String representation of each element in this collection separated by line-feed.



60
61
62
# File 'lib/watir-classic/element_collection.rb', line 60

def to_s
  map { |e| e.to_s }.join("\n")
end