Class: Watir::Table
- 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
Instance Attribute Summary
Attributes included from Container
#activeObjectHighLightColor, #page_container, #type_keys, #typingspeed
Attributes inherited from Element
Class Method Summary collapse
-
.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.).
Instance Method Summary collapse
-
#[](index) ⇒ Object
Returns a row in the table * index - the index of the row.
-
#bodies ⇒ Object
returns a watir object.
-
#body(how, what) ⇒ Object
returns a watir object.
-
#column_count(index = 1) ⇒ Object
This method returns the number of columns in a row of the table.
-
#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.
-
#each ⇒ Object
iterates through the rows in the table.
-
#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.
-
#initialize(container, how, what) ⇒ Table
constructor
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.
- #locate ⇒ Object
-
#row_count ⇒ Object
Returns the number of rows inside the table, including rows in nested tables.
-
#row_count_excluding_nested_tables ⇒ Object
Returns the number of rows in the table, not including rows in nested tables.
-
#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.
-
#to_a(max_depth = 1) ⇒ Object
Returns multi-dimensional array of the cell texts in a table.
-
#to_s ⇒ Object
returns the properties of the object in a string raises an ObjectNotFound exception if the object cannot be found.
Methods included from Container
#area, #areas, #button, #buttons, #cell, #cells, #checkbox, #checkboxes, #dds, #divs, #dls, #dts, #element, #elements, #ems, #file_field, #file_fields, #form, #forms, #frame, #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!, #click_no_wait, #document, #enabled?, #exists?, #fire_event, #flash, #focus, #inspect, #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 |
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
97 98 99 100 |
# File 'lib/watir/table.rb', line 97 def [](index) assert_exists return TableRow.new(@container, :ole_object, _row(index)) end |
#bodies ⇒ Object
returns a watir object
147 148 149 150 |
# File 'lib/watir/table.rb', line 147 def bodies assert_exists return TableBodies.new(@container, @o) end |
#body(how, what) ⇒ Object
returns a watir object
142 143 144 |
# File 'lib/watir/table.rb', line 142 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
118 119 120 121 |
# File 'lib/watir/table.rb', line 118 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
163 164 165 |
# File 'lib/watir/table.rb', line 163 def column_values(columnnumber) return (1..row_count).collect {|i| self[i][columnnumber].text} end |
#each ⇒ Object
iterates through the rows in the table. Yields a TableRow object
88 89 90 91 92 93 |
# File 'lib/watir/table.rb', line 88 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
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/watir/table.rb', line 44 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 |
#locate ⇒ Object
32 33 34 35 36 37 38 39 40 |
# File 'lib/watir/table.rb', line 32 def locate if @how == :xpath @o = @container.element_by_xpath(@what) elsif @how == :ole_object @o = @what else @o = @container.locate_tagged_element('TABLE', @how, @what) end end |
#row_count ⇒ Object
Returns the number of rows inside the table, including rows in nested tables.
103 104 105 106 107 |
# File 'lib/watir/table.rb', line 103 def row_count assert_exists #return table_body.children.length return @o.getElementsByTagName("TR").length end |
#row_count_excluding_nested_tables ⇒ Object
Returns the number of rows in the table, not including rows in nested tables.
110 111 112 113 |
# File 'lib/watir/table.rb', line 110 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
170 171 172 |
# File 'lib/watir/table.rb', line 170 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
127 128 129 130 131 132 133 134 |
# File 'lib/watir/table.rb', line 127 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_s ⇒ Object
returns the properties of the object in a string raises an ObjectNotFound exception if the object cannot be found
80 81 82 83 84 85 |
# File 'lib/watir/table.rb', line 80 def to_s assert_exists r = string_creator r += table_string_creator return r.join("\n") end |