Reputation: 97
I'm using nightwatchjs with the Axios npm package to test that a large list of URLs return the correct status code.
My code looks like this;
var axios = require('axios');
module.exports = {
'@tags': ['siteMapTesting'],
'Sitemap test for Parkers pages': async (browser) => {
var result = await alasql.promise('select URL from xlsx("./testUrls.xlsx",{sheetid:"QA-245-xmlSiteMap"})');
var xcelData = result.map(item => {
return item.URL;
});
async function siteMapUrlsTestArr(item) {
var response = await axios.get(browser.launch_url + item);
console.log('Sitemap test URL =', (browser.launch_url + item + ' status code = ' + response.status));
browser.verify.equal(response.status, 200);
}
for (let index = 0; index < 1000; index++) {
var xmlTestUrl = xcelData[Math.floor(Math.random()*xcelData.length)];
await siteMapUrlsTestArr(xmlTestUrl);
}
},
'Closing the browser': function (browser) {
browser.browserEnd();
},
};
This works fine when the URL returns the expected 200 status code.
However, when it fails, it fails with the following message;
Site Map/ QA 245 Xml Site Map] Sitemap test for Parkers pages (556ms) AxiosError: Request failed with status code 404 at BrotliDecompress.emit (node:events:525:35) at endReadableNT (node:internal/streams/readable:1359:12) at process.processTicksAndRejections (node:internal/process/task_queues:82:21)
TEST FAILURE: 1 error during execution; 0 assertions failed, 1 passed (4.029s)
✖ siteMap/QA-245-xmlSiteMap – Sitemap test for Parkers pages (556ms)
AxiosError: Request failed with status code 404 at BrotliDecompress.emit (node:events:525:35) at endReadableNT (node:internal/streams/readable:1359:12) at process.processTicksAndRejections (node:internal/process/task_queues:82:21)
SKIPPED:
- Closing the browser
As can be seen, there is no mention of the tested URL that caused the failure, so I have no idea which of the 1000 randomly selected URLs has returned a status code other than a 200.
I'd like to console.log
the URL and the status code, but still making sure the test fails with a 404, 500, etc status code.
I've also tried adding a loop into the test script, as below;
var alasql = require('alasql');
var axios = require('axios');
module.exports = {
'@tags': ['siteMapTesting'],
'Sitemap test for Parkers pages': async (browser) => {
var result = await alasql.promise('select URL from xlsx("./testUrls.xlsx",{sheetid:"QA-245-xmlSiteMap"})');
var xcelData = result.map(item => {
return item.URL;
});
async function siteMapUrlsTestArr(item) {
var response = await axios.get(browser.launch_url + item);
if (response.status !== 200) {
throw new Error('Sitemap fail URL =', (browser.launch_url + item + ' status code = ' + response.status));
}
}
for (let index = 0; index < 5; index++) {
var xmlTestUrl = xcelData[Math.floor(Math.random()*xcelData.length)];
await siteMapUrlsTestArr(xmlTestUrl);
}
},
'Closing the browser': function (browser) {
browser.browserEnd();
},
};
but this doesn't list the failing URL either.
Any help would be greatly appreciated. Thanks.
Upvotes: 0
Views: 24
Reputation: 97
I figured this out, not long after posting (sorry!).
This worked for me;
var alasql = require('alasql');
var axios = require('axios');
module.exports = {
'@tags': ['siteMapTesting'],
'Sitemap test for Parkers pages': async (browser) => {
var result = await alasql.promise('select URL from xlsx("./testUrls.xlsx",{sheetid:"QA-245-xmlSiteMap"})');
var xcelData = result.map(item => {
return item.URL;
});
async function siteMapUrlsTestArr(item) {
axios.get(browser.launch_url + item)
.catch(function (error) {
if (error.response) {
console.log(item, error.response.status);
throw new Error();
}
})
}
for (let index = 0; index < 500; index++) {
var xmlTestUrl = xcelData[Math.floor(Math.random()*xcelData.length)];
await siteMapUrlsTestArr(xmlTestUrl);
}
},
'Closing the browser': function (browser) {
browser.browserEnd();
},
};
Upvotes: 0