0

UseCase :

I want to automatically test all sitemap URLs (more than 1000) of our website after each new code release to see if the update broke any of them.

My problem :

Test fails once a URL returns anything else than status 200 (even with failOnStatus: false) but instead I need to save the results in a sort of a callback function and only check them at the end and fail the test at the end (in order to find all the broken links)

My code :

describe('Validate sitemaps files', () => {
let urls = [];
it("Should succesfully load each url in the sitemap", () => {
  cy.fixture('sitemaps.json').then((data) => {
    for (var index in data) {
      cy.log(data[index].url)
      cy.request({
        //url: Cypress.config().baseUrl + data[index].url, failOnStatusCode: false,
        url: data[index].url, failOnStatus: false,
        headers: {
          "Content-Type": "text/xml; charset=utf-8",
          "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36",
        },
      }).as("sitemap").then((response) => {
        urls = Cypress.$(response.body)
          .find("loc")
          .toArray()
          .map((el) => el.innerText
          )
      })
    }
    urls.forEach((url) => {
      // check if the resource exists
      cy.request(url).its('status').then(status => {
        if (status !== 200) {
          failed.push(url)
        }
      }).then(() => {
        // check inside then to ensure loop has finished
        cy.log('Failed links: ' + `${failed.join(', ')}`)
        expect(failed.length).to.eq(0)
      })
      cy.wrap('passed').as('ctrl')
    })
  })
})

})

Fixture (just an example to test my code) :

[

{

  "url": "https://gooogle.com"

},

{

  "url": "https://browserstack.com/notfound"

},

{

  "url": "https://yahoo.com"

},

{

  "url": "https://browserstack.com"

}

]

Test result enter image description here

I have already seen this answer ==> Google but no success so far.

Any help is much appreciated

  • This looks like a typo - you have missed `failOnStatusCode` in the 2nd `cy.request()`. – Fody Oct 20 '22 at 19:40
  • Does this answer your question? [Denial of Service (status code 429) when testing sitemap links in Cypress](https://stackoverflow.com/questions/73751968/denial-of-service-status-code-429-when-testing-sitemap-links-in-cypress) – Fody Oct 20 '22 at 19:41
  • Thank you very much @Fody, I've managed to solve it in another way (more or less) but it works just fine :) – user3095223 Oct 24 '22 at 07:45

0 Answers0