'Exception is not handled in Cypress
I'm using below code to handle exception. But when exception fires, execution is aborted.
it('Validate Login form', function () {
cy.on('uncaught:exception', (err, runnable) => {
done()
return false
})
cy.xpath(repository.discoverMusician.pageHeading);
cy.validateLoginForm(repository);
})
But when exception is fired for cy.xpath(repository.discoverMusician.pageHeading);, the execution is aborted and cy.validateLoginForm(repository); is not executed.
Can someone please help?
Solution 1:[1]
Your problem is likely caused by calling the done callback in your exception handler. The done callback is passed to it test case's callback, and is meant to be called to terminate test early.
First example - terminate test on error, but make it succeed:
describe('test', () => {
it('test', () => {
cy.document().then( doc => {
doc.body.innerHTML = `
<button class="btn" onclick="throw new Error('xx')">
click me
</button>
`;
});
Cypress.on('uncaught:exception', () => {
return false;
});
cy.log('1');
cy.get('.btn').click();
cy.log('2'); // won't run
});
});
second example - don't terminate test on error, but ignore the error instead:
describe('test', () => {
it('test', (done) => {
cy.document().then( doc => {
doc.body.innerHTML = `
<button class="btn" onclick="throw new Error('xx')">
click me
</button>
`;
});
Cypress.on('uncaught:exception', () => {
done();
return false;
});
cy.log('1');
cy.get('.btn').click();
cy.log('2'); // will be run
});
});
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | dwelle |
