I'm testing a search functionality that filters the entered characters against users' Firstname, Lastname, role, or email.
I have a table
with multiple tr
and each tr
has multiple td
.
I want to assert that the returned rows contain a value in any td
that matches the entered keyword.
I created the below helper function but cy.get('td').contains(searchKeyword);
causes the browser to hang up. any idea what could be the solution here.
assertCorrectFilterResults(searchKeyword: string) {
this.elements.tableBody().find("tr").then(rows => {
rows.toArray().forEach(row => {
cy.get('td').contains(searchKeyword);
})
});
};
My helper was inspired from the solution mentioned here How to get the number of Rows in a table & also the row number of a value
In case if you directly want to look into the td
elements you can directly loop over them.
For exact Match with the searchKeyword
cy.get('tbody.MuiTableBody-root tr td').each(($ele) => {
if($ele.text().trim() == searchKeyword){
expect($ele.text().trim()).to.equal(searchKeyword) //Assertion for exact text
}
})
For partial match with the searchKeyword
cy.get('tbody.MuiTableBody-root tr td').each(($ele) => {
if($ele.text().trim().includes(searchKeyword)){
expect($ele.text().trim()).to.include(searchKeyword) //Assertion for partial text
}
})