Class: Watir::ElementCollections

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/watir/element_collections.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 IE

Instance Method Summary collapse

Constructor Details

#initialize(container) ⇒ ElementCollections

Super class for all the iteractor classes

* container - an instance of an IE object


9
10
11
12
13
14
15
16
# File 'lib/watir/element_collections.rb', line 9

def initialize(container)
  @container = container
  @page_container = container.page_container
  @length = length # defined by subclasses
  
  # set up the items we want to display when the show method is used
  set_show_items
end

Instance Method Details

#[](n) ⇒ Object

allows access to a specific item in the collection



49
50
51
# File 'lib/watir/element_collections.rb', line 49

def [](n)
  return iterator_object(n-1)
end

#eachObject

iterate through each of the elements in the collection in turn



44
45
46
# File 'lib/watir/element_collections.rb', line 44

def each
  0.upto(@length-1) { |i| yield iterator_object(i) }
end

#firstObject



53
54
55
# File 'lib/watir/element_collections.rb', line 53

def first
  iterator_object(0)
end

#get_length_of_input_objects(object_type) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/watir/element_collections.rb', line 25

def get_length_of_input_objects(object_type)
  object_types =
  if object_type.kind_of? Array
    object_type
  else
    [object_type]
  end
  
  length = 0
  objects = @container.document.getElementsByTagName("INPUT")
  if objects.length > 0
    objects.each do |o|
      length += 1 if object_types.include?(o.invoke("type").downcase)
    end
  end
  return length
end

#inspectObject



88
89
90
# File 'lib/watir/element_collections.rb', line 88

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

#lastObject



57
58
59
# File 'lib/watir/element_collections.rb', line 57

def last
  iterator_object(length - 1)
end

#showObject

this method is the way to show the objects, normally used from irb



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/watir/element_collections.rb', line 62

def show
  s = "index".ljust(6)
  @show_attributes.each do |attribute_length_pair|
    s += attribute_length_pair.attribute.ljust(attribute_length_pair.length)
  end
  
  index = 1
  self.each do |o|
    s += "\n"
    s += index.to_s.ljust(6)
    @show_attributes.each do |attribute_length_pair|
      begin
        s += eval('o.ole_object.invoke("#{attribute_length_pair.attribute}")').to_s.ljust(attribute_length_pair.length)
      rescue => e
        s += " ".ljust(attribute_length_pair.length)
      end
    end
    index += 1
  end
  puts s
end

#to_sObject



84
85
86
# File 'lib/watir/element_collections.rb', line 84

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