Class: Watir::Table

Inherits:
Element show all
Includes:
Container
Defined in:
lib/watir/table.rb

Overview

This class is used for dealing with tables. Normally a user would not need to create this object as it is returned by the Watir::Container#table method

many of the methods available to this object are inherited from the Element class

Constant Summary

Constants inherited from Element

Element::TO_S_SIZE

Instance Attribute Summary

Attributes included from Container

#activeObjectHighLightColor, #page_container, #type_keys, #typingspeed

Attributes inherited from Element

#container

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Container

#__ole_inner_elements, #input_element_locator, #locator_for, #log, #set_container, #show_all_objects, #tagged_element_locator, #wait

Methods inherited from Element

#<=>, #__ole_inner_elements, #activeObjectHighLightColor, #after_text, #assert_enabled, #assert_exists, #attribute_value, #before_text, #click, #click!, #create_event, #dispatch_event, #document, #enabled?, #exists?, #fire_event, #flash, #focus, inherited, #inspect, #locate, #method_missing, #name, #ole_object, #ole_object=, #parent, #text, #type_keys, #typingspeed, #visible?

Constructor Details

#initialize(container, how, what) ⇒ Table

Returns an initialized instance of a table object

* container      - the container
* how         - symbol - how we access the table
* what         - what we use to access the table - id, name index etc


25
26
27
28
29
30
# File 'lib/watir/table.rb', line 25

def initialize(container, how, what)
  set_container container
  @how = how
  @what = what
  super nil
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Watir::Element

Class Method Details

.create_from_element(container, element) ⇒ Object

Returns the table object containing the element

* container  - an instance of an IE object
* anElement  - a Watir object (TextField, Button, etc.)


14
15
16
17
18
19
# File 'lib/watir/table.rb', line 14

def Table.create_from_element(container, element)
  element.locate if element.respond_to?(:locate)
  o = element.ole_object.parentElement
  o = o.parentElement until o.tagName == 'TABLE'
  new container, :ole_object, o 
end

Instance Method Details

#[](index) ⇒ Object

Returns a row in the table

* index         - the index of the row


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

def [](index)
  assert_exists
  return TableRow.new(@container, :ole_object, _row(index))
end

#bodiesObject

returns a watir object



137
138
139
140
# File 'lib/watir/table.rb', line 137

def bodies
  assert_exists
  return TableBodies.new(@container, @o)
end

#body(how, what) ⇒ Object

returns a watir object



132
133
134
# File 'lib/watir/table.rb', line 132

def body(how, what)
  return TableBody.new(@container, how, what, self)
end

#column_count(index = 0) ⇒ Object

This method returns the number of columns in a row of the table. Raises an UnknownObjectException if the table doesn’t exist.

* index         - the index of the row


108
109
110
111
# File 'lib/watir/table.rb', line 108

def column_count(index=0)
  assert_exists
  _row(index).cells.length
end

#column_values(columnnumber) ⇒ Object

Returns an array containing all the text values in the specified column Raises an UnknownCellException if the specified column does not exist in every Raises an UnknownObjectException if the table doesn’t exist. row of the table

* columnnumber  - column index to extract values from


153
154
155
# File 'lib/watir/table.rb', line 153

def column_values(columnnumber)
  return (0..row_count - 1).collect {|i| self[i][columnnumber].text}
end

#eachObject

iterates through the rows in the table. Yields a TableRow object



78
79
80
81
82
83
# File 'lib/watir/table.rb', line 78

def each
  assert_exists
  0.upto(@o.rows.length - 1) do |i| 
    yield TableRow.new(@container, :ole_object, _row(i))
  end
end

#highlight(set_or_clear) ⇒ Object

override the highlight method, as if the tables rows are set to have a background color, this will override the table background color, and the normal flash method won’t work



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/watir/table.rb', line 34

def highlight(set_or_clear)
  if set_or_clear == :set
    begin
      @original_border = @o.border.to_i
      if @o.border.to_i==1
        @o.border = 2
      else
        @o.border = 1
      end
    rescue
      @original_border = nil
    end
  else
    begin
      @o.border= @original_border unless @original_border == nil
      @original_border = nil
    rescue
      # we could be here for a number of reasons...
    ensure
      @original_border = nil
    end
  end
  super
end

#row_countObject

Returns the number of rows inside the table, including rows in nested tables.



93
94
95
96
97
# File 'lib/watir/table.rb', line 93

def row_count
  assert_exists
  #return table_body.children.length
  return @o.getElementsByTagName("TR").length
end

#row_count_excluding_nested_tablesObject

Returns the number of rows in the table, not including rows in nested tables.



100
101
102
103
# File 'lib/watir/table.rb', line 100

def row_count_excluding_nested_tables
  assert_exists
  return @o.rows.length
end

#row_values(rownumber) ⇒ Object

Returns an array containing all the text values in the specified row Raises an UnknownObjectException if the table doesn’t exist.

* rownumber  - row index to extract values from


160
161
162
# File 'lib/watir/table.rb', line 160

def row_values(rownumber)
  return (0..column_count(rownumber) - 1).collect {|i| self[rownumber][i].text}
end

#to_a(max_depth = 1) ⇒ Object

Returns multi-dimensional array of the cell texts in a table.

Works with tr, th, td elements, colspan, rowspan and nested tables. Takes an optional parameter max_depth, which is by default 1



117
118
119
120
121
122
123
124
# File 'lib/watir/table.rb', line 117

def to_a(max_depth=1)
  assert_exists
  y = []
  @o.rows.each do |row|
    y << TableRow.new(@container, :ole_object, row).to_a(max_depth)
  end
  y
end

#to_sObject

returns the properties of the object in a string raises an ObjectNotFound exception if the object cannot be found



70
71
72
73
74
75
# File 'lib/watir/table.rb', line 70

def to_s
  assert_exists
  r = string_creator
  r += table_string_creator
  return r.join("\n")
end