Class: Celerity::ElementCollection

Included Modules

Enumerable

This class is the superclass for the iterator classes (Buttons, Links, Spans etc.) It would normally only be accessed by the iterator methods (Browser#spans, Browser#links, …).

Constructor Summary

public initialize(container, how = nil, what = nil)

Meta Tags

Parameters:

[View source]


15
16
17
18
19
# File 'lib/celerity/element_collection.rb', line 15

def initialize(container, how = nil, what = nil)
  @container = container
  @object = (how == :object ? what : nil)
  @length = length
end

Public Visibility

Public Instance Method Summary

#[](n)

Get the element at the given index.

Returns: Celerity::Element

#each {|element| ... }
#first

Get the first element in this collection.

Returns: Celerity::Element

#last

Get the last element in this collection.

Returns: Celerity::Element

#length #size

Returns: Fixnum

#to_s

Note: This can be quite useful in irb:.

Returns: String

Public Instance Method Details

[]

public Celerity::Element [](n)

Get the element at the given index. This is 1-indexed to keep compatibility with Watir - subject to change. Also note that because of Watir’s lazy loading, this will return an Element instance even if the index is out of bounds.

Meta Tags

Parameters:

[Fixnum] n

Index of wanted element, 1-indexed.

Returns:

[Celerity::Element]

Returns a subclass of Celerity::Element

[View source]


59
60
61
62
63
64
65
# File 'lib/celerity/element_collection.rb', line 59

def [](n)
  if @elements && @elements[n - Celerity.index_offset]
    element_class.new(@container, :object, @elements[n - Celerity.index_offset])
  else
    iterator_object(n - Celerity.index_offset)
  end
end

each

public each {|element| ... }

Meta Tags

Parameters:

Yield Parameters:

[Celerity::Element] element

Iterate through the elements in this collection.

[View source]


39
40
41
42
43
44
45
46
47
# File 'lib/celerity/element_collection.rb', line 39

def each
  if @elements
    @elements.each { |e| yield(element_class.new(@container, :object, e)) }
  else
    0.upto(@length - 1) { |i| yield iterator_object(i) }
  end

  @length
end

first

public Celerity::Element first

Get the first element in this collection. (Celerity-specific)

Meta Tags

Parameters:

Returns:

[Celerity::Element]

Returns a subclass of Celerity::Element

[View source]


73
74
75
# File 'lib/celerity/element_collection.rb', line 73

def first
  self[Celerity.index_offset]
end

last

public Celerity::Element last

Get the last element in this collection. (Celerity-specific)

Meta Tags

Parameters:

Returns:

[Celerity::Element]

Returns a subclass of Celerity::Element

[View source]


83
84
85
# File 'lib/celerity/element_collection.rb', line 83

def last
  self[Celerity.index_offset - 1]
end

length

public Fixnum length

Also known as: size

Meta Tags

Parameters:

Returns:

[Fixnum]

The number of elements in this collection.

[View source]


25
26
27
28
29
30
31
32
# File 'lib/celerity/element_collection.rb', line 25

def length
  if @object
    @object.length
  else
    @elements ||= ElementLocator.new(@container, element_class).elements_by_idents
    @elements.size
  end
end

to_s

public String to_s

Note: This can be quite useful in irb:

  puts browser.text_fields

Meta Tags

Parameters:

Returns:

[String]

A string representation of all elements in this collection.

[View source]


95
96
97
# File 'lib/celerity/element_collection.rb', line 95

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