简体   繁体   中英

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

You can globally increase the responseTimeout by adding the value in the cypress config file( cypress.json before cypress 10, cypress.config.js after cypress 10). The default is 30 seconds or 30000 ms.

If you want to increase it for just one cy.request , you can do it locally like this:

cy.request('http://re6l.com/', {timeout: 40000})

You can add failOnStatusCode: false to prevent cypress from failing the test if response codes other than 2xx and 3xx.

cy.request('http://re6l.com/', {timeout: 40000, failOnStatusCode: false})

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM