I'm trying to run a for loop within a Puppeteer test that sends values to a search bar:
test('test one', () => {
var results = [""]
var fuzz = ['apple', 'banana', 'kiwi'];
fuzz.forEach(async function(value) {
await page.goto('http://localhost:8080/')
await page.$eval('#searchtext', el => el.value = value);
await page.$eval('#searchform', form => form.submit())
await page.waitForSelector('.results-table');
var text = await page.evaluate(() => document.body.textContent);
results.push(text)
});
expect(results).toEqual(expect.arrayContaining(['bleh']));
});
However, whenever I run this it returns an empty results
array which makes me think the forEach loop isn't finishing.
expect(received).toEqual(expected) // deep equality
Expected: ArrayContaining ["bleh"]
Received: [""]
I know the code within the for loop is correct as I have other tests that aren't looped and do the same thing but return results. Any ideas how to do this cleanly/correctly? Thanks.