Search⌘ K

Table Assertions

Explore how to assert text within HTML tables using Selenium WebDriver in Node.js. Learn techniques to verify table cell and row contents by using element IDs and XPath selectors, enhancing your skills in web test automation.

Assert table text

HTML tables are commonly used for displaying grid data on web pages.

The source code for above HTML table is as follows:

HTML
<table id="aha_table" cellpadding="1" border="1" width="30%">
<tr id="row_1">
<td id="cell_1_1">A</td>
<td id="cell_1_2">B</td>
</tr>
<tr id="row_2">
<td id="cell_2_1">a</td>
<td id="cell_2_2">b</td>
</tr>
</table>

We can assert the text in this table as:

JavaScript (JSX)
driver.findElement(By.id("alpha_table")).getText().then(function(the_elem_text){
assert.equal("A B\na b", the_elem_text);
});
driver.findElement(By.id("alpha_table")).then(function(the_element) {
driver.executeScript("return arguments[0].outerHTML;", the_element).then(
function(the_elem_html) {
assert(the_element_html.contains("<td id=\"cell_1_1\">A</td>"))
});
});

The above script is asserting the text of the first ...