Wednesday, 11 May 2011

Converting scripts from Watir to Webdriver, Table size


I am starting the process of converting my WATIR scripts to use WATIR webdriver.
There a couple of table methods I was using in my WATIR scripts to check the size (rows and columns) of a HTML table.
myTable.row_count
myTable.column_count
These methods don't exist in webdriver so I was looking for a good way of doing the same check.
For rows this seems to give the same result as the row_count method
myTable.rows.length
For column count of the table I've tried converting the table to an array of strings and getting the length of the first row, but the conversion to the string array is taking a while.
myCols = myTable.strings[0].length
After asking the question on StackOverFlow the solution to find the number of columns in the table was to use;
myCols = myTable.row.cells.length
This assumes that the first row has the correct number of columns, which in our application should always be the case.

To avoid rewriting all the unit tests I've extended the Watir::Table class at the start of the script so that it contains the missing row_count and column_count methods.


class Watir::Table
    def row_count
        return self.rows.length
    end
    def column_count
        return self.row.cells.length
    end
end

The unit tests now run unaltered.

If your table has a uniform number of columns for each row then you could check the whole table by doing something like.


        myTable.rows.each { |myRow|
            myCols = myRow.cells.length
            assert_equal(validColumnCount,myCols,"Column count mismatch")
        }




Doodle's Geek Monkey by Alastair Montgomery

0 comments: