Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

309
Views
Cypress Error Catching: how to catch error, warn using cy.log(), then continue with test

so I'm trying to use something like "error catching" to catch an error, cy.log a custom error message, then continue with the test without error catching after that point.

For example, clicking a button that increases quantity in cart, then asserting a change in the quantity #.

  • If the quantity # doesn't update within 2 seconds, warn in console.
  • If the quantity # doesn't update 6 seconds later (for 8 second global default timeout), fail test step
//some sort of error catching here which suppresses the error, and cy.logs 

cy.get("#AddToCart").click()
cy.get("#cart-count", timeout: 2000).should(contain, 2)

//disable error catching here

cy.get("#cart-count", timeout: 6000).should(contain, 2)

I tried something like

cy.on("error", (err, runnable) => {
          expect(err.message).to.include("blue");
          cy.log("this single test failed, but continue other tests");
          return false;
        });
        cy.get(".cy-prc")
          .should("be.visible")
          .invoke("text")
          .should("include", "blue");

Where the .should("include", "blue") should fail. If I can get this to fail and cy.log, then find a way to disable the error catch and do the 2nd assertion, I think I could get this working as I'm hoping it to.

Edit: We're trying to measure potential performance issues within a certain threshold, and have a custom warning printed to console when a threshold is exceeded without failing the test. We have several components or elements that can be affected by small changes in the backend, and we'd like to have warnings when we're approaching an unacceptable threshold before they become failing criteria.

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

There's a couple of command: events that might be useful

const commands = []

function commandStart(cmd) {
  const id = cmd.attributes.id
  commands.push({
    ...cmd.attributes,
    started: +new Date(),
  })
}

function commandEnd(cmd) {
  const entry = commands.filter(c => c.id === cmd.attributes.id)[0]
  entry.ended = +new Date()
  entry.duration = entry.ended - entry.started
}

Cypress.on('command:start', commandStart)
Cypress.on('command:end', commandEnd)

cy.get("#AddToCart").click()
cy.get("#cart-count", timeout: 2000).should(contain, 2)

The commands will look like

enter image description here

so to check the 2nd command

cy.then(() => {
  cy.wrap(commands[1])
    .its('duration')
    .should('be.lt', 6000)

  // turn off command duration
  Cypress.off('command:start', commandStart)
  Cypress.off('command:end', commandEnd)
})
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!