The Issue
Validating HTML tables when testing requires a different approach than other basic web eleements. HTML tables have specific Watir Webdriver commands that allow for a variety of convenient validations, including row/column count and text validation. Lets take a look!
The Answer
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
# row 1, col 1 browser.table(:id => "table")[0][0].text # row 1, col 2 (alternate) browser.table(:id => "table").tr{0}.cell{1}.text # row 2 - entire text puts browser.table(:id => "table")[1].text # click row #4 puts browser.table(:id => "table")[3].click # get column count browser.table(:id => "table").row.cells.length # row count browser.table(:id => "table").row_count browser.table(:id => "table").rows.length |
The above commands span a plethora of options for table interactions. Validations involving row/column count are the jumping off point basic for table validation. Lets use a few of these in an example.
The Code
1 2 3 4 5 6 7 |
#textfieldtest.rb require 'rubygems' require 'watir-webdriver' browser = Watir::Browser.new :firefox browser.goto "http://www.w3schools.com/html/html_tables.asp" puts "Column Count: #{browser.table(:id,"customers").row.cells.length}" puts "Row Count: browser.table(:id,"customers").rows.length" |
This will navigate the browser to the table page and validate the rows/columns.
The Result
We can save the file (textfieldtest.rb) and run it from the command line, with the following result:
- Browser opens
- Browser navigates to the HTML example page
- Validates column amount
- Validates row amount
The Takeaway
Whether you need to validate text or row length, HTML tables that you encounter will have a multitude of variables to test. Tackling these variables requires the utilization of specific Watir Webdriver commands for tables, which will prove useful across your testing.