Friday, November 21, 2008

How to Verify Number of Rows in a Table using Selenium


Hi All,

We can compare number of rows in table with predefined number using verifyTableRows extension. If success it will return true, else it will return false.

user-extension.js:


Selenium.prototype.getTableRows = function(locator) {
/**
* Gets the number of rows in a table.
*
* @param locator element locator for table
* @return number of rows in the table, 0 if none
*/

var table = this.browserbot.findElement(locator);

return table.rows.length.toString();

};

Consider test_get_table_rows.html is the page to test.

<html>
<head>
</head>
<body>
<table id="tableWith3Rows">
<tr>
<td>1</td>
</tr>
<tr>
<td>2</td>
</tr>
<tr>
<td>3</td>
</tr>
</table>
<table id="tableWithNoRows">
</table>
</body>
</html>

by using verifyTableRows we can verify Number of Rows as follows:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>TestTableRows</title>
</head>
<body>
<table cellpadding="1" cellspacing="1" border="1">
<thead>
<tr><td rowspan="1" colspan="3">TestTableRows</td></tr>
</thead><tbody>
<tr>
<td>open</td>
<td>./tests/html/test_table_rows.html</td>
<td></td>
</tr>
<tr>
<td>verifyTableRows</td>
<td>tableWith3Rows</td>
<td>3</td>
</tr>
<tr>
<td>verifyTableRows</td>
<td>tableWithNoRows</td>
<td>0</td>
</tr>

</tbody></table>

</body>

</html>

No comments: