I have test that opens every item on the list. Then it validates if there are no empty (‘–‘) fields. I want to log in console that there were empty fields and go to another item and run the loop till the end. When Cypress throws error tests are stopped.
How can I catch this error and continue running my loop for other items and log errors.
cy.get('body').then(($body) => {
if ($body.find('.MuiTableRow-root.MuiTableRow-hover .MuiTableCell-body').length > 0) { globalActions.getTableRow().then((element) => {
if (element.is(':visible')) {
globalActions.checkProgressBarDoesntExist().then(() => {
cy.get('table[data-testid="table-items"] tbody tr').each(($row) => {
cy.wrap($row).find('.type').invoke('text').then((stageText) => {
if (stageText.includes('Testing')) {
cy.wrap($row).click();
ItemsValues.forEach((field) => {
cy.wrap(null).then(() => {
itemsActions.getValue(field)
.should('not.contain', '--')
.catch((err) => {
cy.task('log', `DOESNT WORK: ${err.message}`);
});
});
});
}
});
});
});
}
});
}
});
When I try this I get error : Expected .. not to contain —
2
Answers
There is no
.catch
in Cypress. This is by design since test execution is supposed to be predictable and relies on catching errors, which implies the app may be in an unknown state and is no longer safe to continue.However, you know that it is safe to continue. So you can change the test so that we instead collect a list of "bad" fields and call "should()" on this at the end of the loop. Inside the loop, we log each time a bad field is encountered, but we don’t assert on it right there.
To stop the error Expected .. not to contain "–" you can change the
should()
into athen()
.When you use
should()
Cypress will stop with a test failure if the condition isn’t met.If you substitute
then()
you can take whatever action you want inside the callback, and as long as it’s notexpect()
orassert()
the test won’t fail, it will carry on.