I am writing a set of tests on a list of elements (HTML table).
Since the tests are still a work in progress, I'm getting a lot of junk data which I'd like to cleanup before each run.
So, I'm trying to delete all rows in before() hook. I imagine the process for deleting should be:
- Search products by name
- Click a delete icon in first row
- Wait for confirmation dialog
- Click "Yes"
- Repeat from step
2until there are no rows left
This is the function I've got so far that I call after typing in the search input:
function deleteAllFoundProducts() {
cy.get('tbody tr').then(rows => {
if (rows.length < 1) return // if no rows remain, terminate recursion
cy.get('span[title="Delete"]', {timeout: 10000}).first().click()
cy.get('.tBtn[title="Yes"]').click().then(() => {
cy.get('.loader', {timeout: 10000}).should('not.exist') // wait for list to reappear
deleteAllFoundProducts() // try again
})
})
}
The problem is – the function continues to run even after deleting the final row, and errors out because there is no span[title="Delete"].
[EDIT]
On closer inspection I realized that an empty table still has one row – with No records found text. I thought it wasn't a row at all. So, the error is caused by having a row that doesn't contain a delete icon. I should have noticed that sooner.
This is relevant HTML with unnecessary Angular stuff removed:
<tbody class="p-element p-datatable-tbody" ng-reflect-value="" ng-reflect-template="[object Object]">
<tr>
<td colspan="6">
<my-loader ng-reflect-loading="false">
</my-loader>
<div class="p-2">No matching records found. </div>
</td>
</tr>
</tbody>
The solution would, I guess, be to check if the row contains an element with text 'No records found', and perform delete only if it doesn't. But I'm not sure how to do it. Any help would still be appreciated.