In general, the table consists of rows and columns which could be formed using td and tr or some other HTML tags. The below image is an example of a table that comprises rows and columns.
But depending on the data they are classified as
With help of CypressIO, we can perform certain operations on the table. They are:
To get access to the rows, tr is used as a CSS.
cy.get("tr");
//selects the total number of rowsTo get access to the column elements, tdortr tdis used as a CSS.
cy.get("td")
[OR]
cy.get("tr td");
//selects the column elements td:nth-child(column number)ortr td:nth-child(column number) is used as the CSS . In the below snippet we are accessing the second column elements using the CSS td:nth-child(2)
.each() the method is used to iterate the elements in an array. Here we are iterating through the second column elements.each() method to iterate in an array.cy.get("td:nth-child(2)").each(($e1, index, $list) => {
const text = $e1.text();
if (text.includes("Google")) {
cy.get("td:nth-child(2)")
.eq(index)
.then(function(Field) {
const Fieldtext = Field.text();
expect(Fieldtext).to.equal("Google");
})
})Now, let us put together all the snippets in a single program.
Example program :
describe("Test suite", function () {
it("Handling tables", function () {
cy.visit("https://chercher.tech/practice/table");
Cypress.on("uncaught:exception", (err, runnable) => {
// returning false here prevents Cypress from
// failing the test
return false;
});
//selects the total number of rows
cy.get("tr");
//selects the column elements
cy.get("td"); // [OR] cy.get("tr td");
//iterating through the array of elements
cy.get("td:nth-child(2)").each(($e1, index, $list) => {
// cy.get("tr td:nth-child(2)").each(($e1, index, $list) => { this can also be used because "td" or "tr td" gives the same output
//storing the content in the text variable
const text = $e1.text();
//If the content is Google,iteration stops
if (text.includes("Google")) {
//gets the CSS of the second column
cy.get("td:nth-child(2)")
//grabs the content in the index value
.eq(index)
// promises should be resolved manually since text() is jQuery method
.then(function (Field) {
const Fieldtext = Field.text();
//comparing strings using the jQuery method expect()
expect(Fieldtext).to.equal("Google");
});
}
});
});
});
Output :
Handling the dynamic table is also like handling the static tables, both have the same program logic. But the data changes periodically in dynamic data tables. However, if we want to select a checkbox related to a particular item then we have to depend on some static item in the row.

.eq(index) and the box is checked.each() method.cy.get("input").eq(index).check();Example program :
describe("Test suite", function () {
it("Handling tables", function () {
cy.visit("https://chercher.tech/practice/dynamic-table");
Cypress.on("uncaught:exception", (err, runnable) => {
// returning false here prevents Cypress from
// failing the test
return false;
});
//selects the total number of rows
cy.get("tr");
//selects the column elements
cy.get("td"); //[OR] cy.get("tr td");
//iterating through the array of elements
cy.get("td:nth-child(2)").each(($e1, index, $list) => {
// cy.get("tr td:nth-child(2)").each(($e1, index, $list) => { this can also be used because "td" or "tr td" gives the same output
//storing the content in the text variable
const text = $e1.text();
//If the content is Google,iteration stops
if (text.includes("google.com")) {
//grabs the element at the index
cy.get("td:nth-child(2)").eq(index);
//selects the checkbox
cy.get("input").eq(index).check();
}
});
});
});
Output :
Thenext() and prev() commands are mostly used while handling the tables to grab the CSS of the sibling cell and the preceding cell/parent cell. The below image shows, what is a parent cell, child cell, and sibling cell.
Let us learn how to use these commands with the help of an example program,
Example program for next()command :
describe("Test suite", function () {
it("Handling tables", function () {
cy.visit("https://chercher.tech/practice/table");
Cypress.on("uncaught:exception", (err, runnable) => {
// returning false here prevents Cypress from
// failing the test
return false;
});
//selects the total number of rows
cy.get("tr");
//selects the column elements
cy.get("td"); // [OR] cy.get("tr td");
//iterating through the array of elements
cy.get("td:nth-child(2)").each(($e1, index, $list) => {
// cy.get("tr td:nth-child(2)").each(($e1, index, $list) => { this can also be used because "td" or "tr td" gives the same output
//storing the content in the text variable
const text = $e1.text();
//If the content is Google,iteration stops
if (text.includes("Google")) {
//gets the CSS of the second column
cy.get("td:nth-child(2)")
//grabs the content in the index value
.eq(index)
//grabs the sibling cell CSS
.next()
// promises should be resolved manually since text() is jQuery method
.then(function (Field) {
const Fieldtext = Field.text();
//comparing strings using the jQuery method expect()
expect(Fieldtext).to.equal("Search Engine");
});
}
});
});
});
Output :
Example program for prev()command :
describe("Test suite", function () {
it("Handling tables", function () {
cy.visit("https://chercher.tech/practice/table");
Cypress.on("uncaught:exception", (err, runnable) => {
// returning false here prevents Cypress from
// failing the test
return false;
});
//selects the total number of rows
cy.get("tr");
//selects the column elements
cy.get("td"); // [OR] cy.get("tr td");
//iterating through the array of elements
cy.get("td:nth-child(2)").each(($e1, index, $list) => {
// cy.get("tr td:nth-child(2)").each(($e1, index, $list) => { this can also be used because "td" or "tr td" gives the same output
//storing the content in the text variable
const text = $e1.text();
//If the content is Google,iteration stops
if (text.includes("Google")) {
//gets the CSS of the second column
cy.get("td:nth-child(2)")
//grabs the content in the index value
.eq(index)
//grabs the previous cell CSS
.prev()
// promises should be resolved manually since text() is jQuery method
.then(function (Field) {
const Fieldtext = Field.text();
//comparing strings using the jQuery method expect()
expect(Fieldtext).to.equal("google.com");
});
}
});
});
});Output :
I am Tharani N V, a Content writer, and SEO specialist. I have a year of experience in both technical and non-technical content writing.