There is an answer here Cypress.io - sitemap.xml validation test that may start you off.
Instead of cy.visit(url)
use cy.request(url)
, it should be much faster.
it('check each url in the sitemap.xml', () => {
const results = []
cy.request('sitemap.xml')
.then(response => {
// convert sitemap xml body to an array of urls
urls = Cypress.$(response.body)
.find('loc')
.toArray()
.map(el => el.innerText)
return urls
})
.then(urls => {
urls.forEach(url => {
cy.request({
url,
failOnStatusCode: false // get status good and bad
})
.then(response => {
results.push({url, statusCode: response.statusCode})
})
})
})
// use results inside .then()
cy.then(() => {
console.log(results)
})
})
BTW putting each URL in a separate test is a big headache.
The above pattern will give you status codes on good and bad URLS, and will be faster than separate tests.