Search code examples
urlcypresshttp-status-codesend-to-end

Cypress: Is it possible to complete a test after failure


Overview

I want to automatically test all 200 pages of our website every week after the update to see if the update broke any of them

The test case

  • Login and go to the sitemap
  • Get the URLs and check each for their HTTP status

The code

    it('check HTTP status', () => { 
        cy.visit(Cypress.config('siteMapUrl'))
        
        cy.get('.portlet-separator').should('contain', 'Available Links')
        
        cy.get('.inputwrapper>a')
            .each(($el, index, $list) => {
                if($list){
                    cy.get($el)
                        .invoke('attr', 'href')
                        .then(href => {
                            cy.request(Cypress.config('url')+href)
                            .should('have.property', 'status', 200)
                        })
                }
        })

What happens:

Once an URL returns anything else than status 200 the test fails.

What I would like:

I would like Cypress to iterate through the complete list of URLs before returning the URLs that failed.

Why?

If more than one URL in the list is broken, I will not find the 2nd broken URL with this test until our devs have fixed the first one. Yet I need to produce a list with all broken URLs at the beginning of the week

I have already seen this answer but I would like to know if there is a different solution before I try to implement this


Solution

  • You should not use .should() after each URL - that will fail immediately, even if setting failOnStatus: false.

    Instead save the results and check at the end.

    const failed = []
    
    cy.get(".inputwrapper>a").each(($el, index, $list) => { 
      cy.get($el)
        .invoke("attr", "href")
        .then((href) => {
          cy.request({
            url: Cypress.config("url") + href,
            failOnStatusCode: false,      
          })
          .its('status')
          .then(status => {
            if (status !== 200) {
              failed.push(href)
            }
          })
        })
      }
    })
    .then(() => {
      // check inside then to ensure loop has finished
      cy.log(`Failed links: `${failed.join(', ')`)
      expect(failed.length).to.eq(0)
    })