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

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 = subclass.to_s.demodulize
  method_name = class_name.underscore
  element_class_name = class_name.singularize

  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)


152
153
154
# File 'lib/firewatir/element_collections.rb', line 152

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

#eachObject

Description:

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


139
140
141
142
143
# File 'lib/firewatir/element_collections.rb', line 139

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

#firstObject

Returns the first element in the collection.



160
161
162
# File 'lib/firewatir/element_collections.rb', line 160

def first
  @element_objects.first
end

#inspectObject



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

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.



168
169
170
# File 'lib/firewatir/element_collections.rb', line 168

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.


130
131
132
# File 'lib/firewatir/element_collections.rb', line 130

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



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

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