Serge
Serge

Reputation: 331

How to catch Cypress request timeout error?

Is it possible to handle request timeouts in Cypress? I don't want error to be thrown. Request for example:

cy.request('http://re6l.com/')

At the moment I suppose that's not possible and I should use another methods for network requests

Upvotes: 5

Views: 2622

Answers (1)

agoff
agoff

Reputation: 7163

To test for all of the bad links on the page, and output all bad links, I'd imagine that adding failOnStatusCode: false will get you most of the way there.

let badLinks = [];
cy.get('a') // get all anchor tags
  .each(($a) => {
    const href = $a.attr('href'); // get the href attribute
    cy.request(href, {failOnStatusCode: false})
      .then((res) => {
        if (res.statusCode >= 400) { // check if the call failed
          badLinks.push(href) // add the failed href to the badLinks array
        }
    });
  })
  .log(badLinks); // log the badLinks

Upvotes: 1

Related Questions