the above solution will work, but it will be serialized i.e. you will get the result of the driver.get promise and then analyze the result of one url before moving to the next. maybe you could use promise.all to do it all in parallel. Something on the lines of
function executeGetPromises() {
var getPromises = [];
var drivers = [];
for (const url of [url1, url2, url3]) {
var driver = new WebDriver.Builder().forBrowser('firefox').build();
getPromises.push(driver.get(url));
drivers.push(driver);
}
var analysePromises = [];
int index = 0;
Promise.all(getPromises.map(p => p.catch(e => e)))
.then(results => {
for (int i=0; i< results.length; i++) {
var result = results[i];
if (!(result instanceof Error)) {
analysePromises.push(AxeBuilder(drivers[i]).analyze);
}
}
executeAnalysePromises(analysePromises);
});
}
function executeAnalysePromises (analysePromises) {
Promise.all(analysePromises.map(p => p.catch(e => e)))
.then(results => {
results.forEach(result => {
if (!(result instanceof Error)) {
console.log(result);
}
});
});
}
here we are keeping a track of all the drivers, and all the driver.get promises are run in parallel and once all the getPromises return (either resolved/rejected), the analysePromises get executed in parallel.
EDIT: A more simpler approach using async functions. The above is a bit complicated, you can achieve the same using async functions
async function executeTask (driver, url) {
try{
await driver.get(url);
let result = await AxeBuilder(driver).analyze();
return Promise.resolve(result);
}
catch(err) {
return Promise.reject(err);
}
}
function iterateThroughUrls(urls) {
urls.forEach(url => {
var driver = new WebDriver.Builder().forBrowser('firefox').build();
executeTask(driver, url).then(result => {
console.log(result);
}).catch(err => {
//handle errors
});
});
}
awaiteachPromiseor usePromise.all?