Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to handle cypress erros?

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

cy.get('button').contains('hello')
  .catch((err) => {
    // oh no the button wasn't found
    // (or something else failed)
    cy.get('somethingElse').click()
  })

>Solution :

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).

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading