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

#area, #areas, #button, #buttons, #cell, #cells, #checkbox, #checkboxes, #dds, #divs, #dls, #dts, #element, #element_by_css, #elements, #ems, #file_field, #file_fields, #form, #forms, #frame, #frames, #hidden, #hiddens, #image, #images, #labels, #link, #links, #lis, #locate_all_elements, #locate_input_element, #locate_tagged_element, #log, #map, #maps, #modal_dialog, #popup, #pres, #ps, #radio, #radios, #row, #rows, #select_list, #select_lists, #set_container, #show_all_objects, #spans, #strongs, #table, #tables, #text_field, #text_fields, #wait

Methods inherited from Element

#<=>, #activeObjectHighLightColor, #after_text, #assert_enabled, #assert_exists, #attribute_value, #before_text, #click, #click!, #document, #enabled?, #exists?, #fire_event, #flash, #focus, #inspect, #method_missing, #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


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

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

#bodiesObject

returns a watir object



149
150
151
152
# File 'lib/watir/table.rb', line 149

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

#body(how, what) ⇒ Object

returns a watir object



144
145
146
# File 'lib/watir/table.rb', line 144

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

#column_count(index = 1) ⇒ 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


120
121
122
123
# File 'lib/watir/table.rb', line 120

def column_count(index=1)
  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


165
166
167
# File 'lib/watir/table.rb', line 165

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

#eachObject

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



90
91
92
93
94
95
# File 'lib/watir/table.rb', line 90

def each
  assert_exists
  1.upto(@o.rows.length) 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



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/watir/table.rb', line 46

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

#locateObject



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/watir/table.rb', line 32

def locate
  if @how == :xpath
    @o = @container.element_by_xpath(@what)
  elsif @how == :css
    @o = @container.element_by_css(@what)
  elsif @how == :ole_object
    @o = @what
  else
    @o = @container.locate_tagged_element('TABLE', @how, @what)
  end
end

#row_countObject

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



105
106
107
108
109
# File 'lib/watir/table.rb', line 105

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.



112
113
114
115
# File 'lib/watir/table.rb', line 112

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


172
173
174
# File 'lib/watir/table.rb', line 172

def row_values(rownumber)
  return (1..column_count(rownumber)).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



129
130
131
132
133
134
135
136
# File 'lib/watir/table.rb', line 129

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



82
83
84
85
86
87
# File 'lib/watir/table.rb', line 82

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