Class: FireWatir::ElementCollections

Inherits:
Object
  • Object
show all
Includes:
Enumerable, JsshSocket
Defined in:
lib/firewatir/element_collections.rb

Overview

Description:

Class for iterating over elements of common type like links, images, divs etc.

Constant Summary collapse

@@current_level =
0

Class Method Summary collapse

Instance Method Summary collapse

Methods included from JsshSocket

#js_eval, #js_eval_method, #jssh_socket, #read_socket, #valid_js_identifier?

Constructor Details

#initialize(container) ⇒ ElementCollections

Returns a new instance of ElementCollections.



25
26
27
28
29
30
31
32
33
34
# File 'lib/firewatir/element_collections.rb', line 25

def initialize(container)
  @container = container
  elements = locate_elements
  length = elements.length
  #puts "length is : #{length}"

  @element_objects = Array.new(length)
  for i in 0..length - 1 do
    @element_objects[i] = element_class.new(container, :jssh_name, elements[i])
  end
end

Class Method Details

.inherited(subclass) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/firewatir/element_collections.rb', line 11

def self.inherited subclass
  class_name = Watir::Util.demodulize(subclass.to_s)
  method_name = Watir::Util.underscore(class_name)
  element_class_name = Watir::Util.singularize(class_name)

  FireWatir::Container.module_eval "def #{method_name}
  locate if respond_to?(:locate)
  return #{class_name}.new(self); end"

  subclass.class_eval "def element_class; #{element_class_name}; end"
end

Instance Method Details

#[](n) ⇒ Object

Description:

Accesses nth element of same tag and type found on the page.

Input:

n - index of element (1 based)


156
157
158
# File 'lib/firewatir/element_collections.rb', line 156

def [](n)
  return @element_objects[n-1]
end

#eachObject

Description:

Iterate over the elements of same tag and type found on the page.


143
144
145
146
147
# File 'lib/firewatir/element_collections.rb', line 143

def each
  for i in 0..@element_objects.length - 1
    yield @element_objects[i]
  end
end

#firstObject

Returns the first element in the collection.



164
165
166
# File 'lib/firewatir/element_collections.rb', line 164

def first
  @element_objects.first
end

#inspectObject



180
181
182
183
# File 'lib/firewatir/element_collections.rb', line 180

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

#lastObject

Returns the last element in the collection.



172
173
174
# File 'lib/firewatir/element_collections.rb', line 172

def last
  @element_objects.last
end

#lengthObject Also known as: size

Description:

Gets the length of elements of same tag and type found on the page.

Ouput:

Count of elements found on the page.


134
135
136
# File 'lib/firewatir/element_collections.rb', line 134

def length
  return @element_objects.length
end

#locate_elementsObject

default implementation. overridden by some subclasses.



37
38
39
# File 'lib/firewatir/element_collections.rb', line 37

def locate_elements
  locate_tagged_elements(element_class::TAG)
end

#to_sObject



176
177
178
# File 'lib/firewatir/element_collections.rb', line 176

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