Let's say I have simple test:
/// <reference types="cypress" />
describe('Something important', () => {
it('First test', () => {
...
})
})
And what I need to do is to handle if something happens in First test
, I was trying something like, but it doesn't work this way:
it('First test', () => {
try {
...
} catch (e) {
... // send email, for example
}
})
So, in some way I need to handle whatever happens in test. Is it possible? Something like this, but for whole test:
cy.get('button').contains('hello')
.catch((err) => {
// oh no the button wasn't found
// (or something else failed)
cy.get('somethingElse').click()
})
CodePudding user response:
Is this what you need Catching Test Failures?
Cypress.on('fail', (error, runnable) => {
debugger
// we now have access to the err instance
// and the mocha runnable this failed on
throw error // throw error to have test still fail
})
This will fire when the test fails, so you can perform your email or other action.
But generally Cypress expects you to know that the button will be there. You shouldn't have too many unpredictable things on the page (or not on the page, as the case may be).