lib/watir/table.rb in watir-3.0.0.rc2 vs lib/watir/table.rb in watir-3.0.0.rc3

- old
+ new

@@ -1,38 +1,80 @@ module Watir - module RowContainer + module TableContainer # Returns a row in the table # * index - the index of the row def [](index) assert_exists - TableRow.new(self, :ole_object, @o.rows.item(index)) + TableRow.new(self, :ole_object => @o.rows.item(index)) end def strings assert_exists rows_memo = [] @o.rows.each do |row| cells_memo = [] row.cells.each do |cell| - cells_memo << TableCell.new(self, :ole_object, cell).text.gsub("\r\n","") + cells_memo << TableCell.new(self, :ole_object => cell).text.gsub("\r\n","") end rows_memo << cells_memo end rows_memo end + end + module TableElementsContainer + def table_elements(klass, tags, how, what, ole_collection) + specifiers = format_specifiers(tags, how, what) + klass.new(self, specifiers, ole_collection) + end + + private :table_elements end + module TableCellsContainer + include TableElementsContainer + + def cells(how={}, what=nil) + assert_exists + table_elements(TableCellCollection, [:th, :td], how, what, @o.cells) + end + + def cell(how={}, what=nil) + specifiers = format_specifiers([:th, :td], how, what) + index = specifiers.delete(:index) || 0 + cells(specifiers)[index] + end + end + + module TableRowsContainer + include TableElementsContainer + + def rows(how={}, what=nil) + assert_exists + table_elements(TableRowCollection, [:tr], how, what, @o.rows) + end + + def row(how={}, what=nil) + specifiers = format_specifiers([:tr], how, what) + index = specifiers.delete(:index) || 0 + rows(specifiers)[index] + end + end + # 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 # - class Table < NonControlElement - include RowContainer + class Table < Element + include TableContainer + include TableRowsContainer + include TableCellsContainer + attr_ole :rules + # 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 def highlight(set_or_clear) if set_or_clear == :set begin @@ -74,18 +116,10 @@ r = string_creator r += table_string_creator return r.join("\n") end - # iterates through the rows in the table. Yields a TableRow object - def each - assert_exists - @o.rows.each do |row| - yield TableRow.new(self, :ole_object, row) - end - end - # Returns the number of rows inside the table, including rows in nested tables. def row_count assert_exists rows.length end @@ -93,11 +127,11 @@ # 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 def column_count(index=0) assert_exists - row[index].cells.length + rows[index].cells.length end # 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. @@ -117,11 +151,11 @@ def hashes assert_exists headers = [] @o.rows.item(0).cells.each do |cell| - headers << TableCell.new(self, :ole_object, cell).text + headers << TableCell.new(self, :ole_object => cell).text end rows_memo = [] i = 0 @o.rows.each do |row| @@ -131,60 +165,30 @@ 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 + cells_memo[headers[j]] = TableCell.new(self, :ole_object => cell).text j += 1 end rows_memo << cells_memo i += 1 end rows_memo end end - class TableSection < NonControlElement - include RowContainer - - Watir::Container.module_eval do - def tbody(how={}, what=nil) - how = {how => what} if what - TableSection.new(self, how.merge(:tag_name => "tbody"), nil) - end - - def tbodys(how={}, what=nil) - how = {how => what} if what - TableSectionCollection.new(self, how.merge(:tag_name => "tbody"), nil) - end - - def thead(how={}, what=nil) - how = {how => what} if what - TableSection.new(self, how.merge(:tag_name => "thead"), nil) - end - - def theads(how={}, what=nil) - how = {how => what} if what - TableSectionCollection.new(self, how.merge(:tag_name => "thead"), nil) - end - - def tfoot(how={}, what=nil) - how = {how => what} if what - TableSection.new(self, how.merge(:tag_name => "tfoot"), nil) - end - - def tfoots(how={}, what=nil) - how = {how => what} if what - TableSectionCollection.new(self, how.merge(:tag_name => "tfoot"), nil) - end - end + class TableSection < Element + include TableContainer + include TableRowsContainer + include TableCellsContainer end - class TableRow < NonControlElement - TAG = "TR" - + class TableRow < Element + include TableCellsContainer + # this method iterates through each of the cells in the row. Yields a TableCell object def each locate cells.each {|cell| yield cell} end @@ -196,57 +200,25 @@ raise UnknownCellException, "Unable to locate a cell at index #{index}" end return cells[index] end - # defaults all missing methods to the array of elements, to be able to - # use the row as an array - # def method_missing(aSymbol, *args) - # return @o.send(aSymbol, *args) - # end def column_count - locate + assert_exists cells.length end - Watir::Container.module_eval do - def row(how={}, what=nil) - TableRow.new(self, how, what) - end - - alias_method :tr, :row - - def rows(how={}, what=nil) - TableRows.new(self, how, what) - end - - alias_method :trs, :rows - end end # this class is a table cell - when called via the Table object - class TableCell < NonControlElement - TAGS = ["TH", "TD"] + class TableCell < Element + attr_ole :headers - alias to_s text + alias_method :to_s, :text def colspan locate @o.colSpan - end - - Watir::Container.module_eval do - def cell(how={}, what=nil) - TableCell.new(self, how, what) - end - - alias_method :td, :cell - - def cells(how={}, what=nil) - TableCells.new(self, how, what) - end - - alias_method :tds, :cells end end end