How to check all links in cypress without stopping in case of an error Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of How to check all links in cypress without stopping in case of an error without wasting too much if your time.

The question is published on by Tutorial Guruji team.

So basicly I have to test many many links on many many sites so I want to write a quite general code. In this case its just checking in the main section on purpose.

it('check all links in main', () => {

    cy.visit('/')
    cy.get("main").within(() => {
        cy.get("a").each(page => {
            cy.request(page.prop('href'))
        })
    })
});

This is checking all links but only as long as there are no errors. I want it to run through the whole page and just log out for every link if it works or not. Its not supposed to stop after the first error.

Answer

It looks like you are looking for the failOnStatusCode option

cy.get("a").each(page => {
  const link = page.prop('href')
  cy.request({
    url: link,
    failOnStatusCode: false  // allow good and bad response to pass into then
  }).then(response => {
    Cypress.log({
      name: link,
      message: response.status
    })
  })
})
We are here to answer your question about How to check all links in cypress without stopping in case of an error - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji