I have a set of files in a folder with specific names and I use node to return me the names of those files and then for each file I want to run a puppeteer test which fills the form and submits it but I seem to be having issues with the forEach not waiting for the previous test to finish and end up with loads of Execution context was destroyed
which obviously prevent my tests from finishing. I ended up with users logged in but the page the page.type
is not run in some of the cases...
How do I wait for the puppeteer to complete the test and then run the next element in forEach?
See code below:
fs.readdir(testFolder, async (err, files) => {
files.forEach(file => {
(async () => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
page.setViewport({
width: 1920,
height: 1080
});
await page.goto('www.mypage.com');
//login
// some login code here
await page.click('#register')
await page.waitForNavigation();
console.log('New Page URL:', page.url());
await page.waitForSelector('#firstName');
await page.waitFor(1000);
await page.type('#firstName', file);
// register and wait for nagivation (in my case it is a page reload)
await page.waitForSelector('#updateProfile');
await page.click('#updateProfile');
await page.waitForNavigation();
await browser.close();
})();
});
});