Replace Table#row_count and Table#column_count with an ElementCollection#count

Given a table:

<table>
  <tr>
    <td>Row 1 - Column 1</td>
    <td>Row 1 - Column 2</td>
  </tr>
  <tr>
    <td>Row 2 - Column 1</td>
    <td>Row 2 - Column 2</td>
  </tr>
  <tr>
    <td>Row 3 - Column 1</td>
    <td>Row 3 - Column 2</td>
  </tr>
</table>

You might want to check the number of rows and/or columns.

Obsolete Method

In Watir-Classic you may have retrieved these values using the row_count and column_count methods.

browser.table.row_count
#=> 3
 
browser.table.column_count
#=> 2

However, these methods will give an error in Watir-Webdriver:

browser.table.row_count
#=> NoMethodError
 
browser.table.column_count
#=> NoMethodError

Preferred Method

For Watir-Webdriver, you will need to count the tr and td elements using an elemnent collection:

browser.table.trs.count
#=> 3
 
browser.table.tr.tds.count
#=> 2

Note that this will return all tr and td elements in the table. Watir-Classic’s method ignored the nested tables, therefore, for nested tables, the equivalent Watir-Webdriver method will need to use the rows and cells methods:

browser.table.rows.count
#=> 3
 
browser.table.row.cells.count
#=> 2
Advertisements
This entry was posted in Watir, Watir Migration, Watir-Classic. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s