Using TypeScript with Cypress for URL extraction.
I have a code objective to extract and validate all URLs in the /sitemap.xml file using cy.request() to check for a status of 200.
The initial version successfully achieves this:
describe('Sitemap Urls', () => {
let urls: any[] = [];
beforeEach(() => {
cy.request({
method: 'GET',
url: 'https://docs.cypress.io/sitemap.xml',
}).then(response => {
expect(response.status).to.eq(200);
urls = Cypress.$(response.body)
.find('loc')
.toArray()
.map(el => el.textContent);
cy.log('Array of Urls: ', urls);
});
});
it(`Validate response of each URL in the sitemap`, () => {
urls.forEach((uniqueUrl: any) => {
cy.request(uniqueUrl).then(requestResponse => {
expect(requestResponse.status).to.eq(200);
});
});
});
});
However, I'd like each request to be its own separate test rather than grouped together in one. My attempt at modifying the code doesn't produce the desired outcome:
describe('Sitemap Urls', () => {
let urls: any[] = ['/'];
beforeEach(() => {
cy.request({
method: 'GET',
url: 'https://docs.cypress.io/sitemap.xml',
}).then(response => {
expect(response.status).to.eq(200);
urls = Cypress.$(response.body)
.find('loc')
.toArray()
.map(el => el.textContent);
cy.log('Array of Urls: ', urls);
});
});
urls.forEach((uniqueUrl: any) => {
it(`Validate response of each URL in the sitemap - ${uniqueUrl}`, () => {
cy.request(uniqueUrl).then(requestResponse => {
expect(requestResponse.status).to.eq(200);
});
});
});
});
Even though the debugger confirms that urls.forEach() has populated all the URLs in the array, the test cases are not executing individually as intended. Any suggestions on what might be causing this issue?