Class: Watir::Table

Inherits:
Element show all
Includes:
TableCellsContainer, TableContainer, TableRowsContainer
Defined in:
lib/watir-classic/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 inherited from Element

#container

Attributes included from Container

#activeObjectHighLightColor, #page_container, #type_keys, #typingspeed

Instance Method Summary collapse

Methods included from TableCellsContainer

#cell, #cells

Methods included from TableRowsContainer

#row, #rows

Methods included from TableContainer

#[], #strings

Methods inherited from Element

#<=>, #__ole_inner_elements, #activeObjectHighLightColor, #assert_enabled, #assert_exists, #attribute_value, #click, #click!, #create_event, #disabled?, #dispatch_event, #document, #double_click, #enabled?, #exists?, #fire_event, #flash, #focus, #focused?, #initialize, #inspect, #locate, #method_missing, #ole_object, #ole_object=, #parent, #right_click, #send_keys, #style, #tag_name, #text, #to_subtype, #type_keys, #typingspeed, #visible?

Methods included from DragAndDropHelper

#drag_and_drop_by, #drag_and_drop_on

Methods included from Container

#__ole_inner_elements, #alert, #locator_for, #modal_dialog, #set_container, support_element, #wait

Methods included from Exception

message_for_unable_to_locate

Methods included from ElementExtensions

#present?, #wait_until_present, #wait_while_present, #when_present

Constructor Details

This class inherits a constructor from Watir::Element

Dynamic Method Handling

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

Instance Method Details

#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


130
131
132
133
# File 'lib/watir-classic/table.rb', line 130

def column_count(index=0)
  assert_exists
  rows[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


140
141
142
# File 'lib/watir-classic/table.rb', line 140

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

#hashesObject



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/watir-classic/table.rb', line 151

def hashes
  assert_exists

  headers = []
  @o.rows.item(0).cells.each do |cell|
    headers << TableCell.new(self, :ole_object => cell).text
  end

  rows_memo = []
  i = 0
  @o.rows.each do |row|
    next if row.uniqueID == @o.rows.item(0).uniqueID

    cells_memo = {}
    cells = row.cells
    raise "row at index #{i} has #{cells.length} cells, expected #{headers.length}" if cells.length < headers.length

    j = 0
    cells.each do |cell|
      cells_memo[headers[j]] = TableCell.new(self, :ole_object => cell).text
      j += 1
    end

    rows_memo << cells_memo
    i += 1
  end
  rows_memo
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



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/watir-classic/table.rb', line 78

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.



122
123
124
125
# File 'lib/watir-classic/table.rb', line 122

def row_count
  assert_exists
  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


147
148
149
# File 'lib/watir-classic/table.rb', line 147

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

#to_sObject

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



114
115
116
117
118
119
# File 'lib/watir-classic/table.rb', line 114

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