I'm trying to run assertions inside Puppeteer's page.on('response') event, however, the following error is thrown and the test wrongly passes: UnhandledPromiseRejectionWarning: AssertionError.
I've read that returning a promise should do the trick, but how can I resolve the promise if I don't know when the event will stop being emitted?
Here is the complete example code:
const assert = require('assert');
const puppeteer = require('puppeteer');
const config = require('../config.json');
describe('Tests', function () {
let browser;
let page;
this.timeout(30000);
before(async function () {
browser = await puppeteer.launch({
headless: config.headless
});
page = await browser.newPage();
});
after(async function () {
await browser.close();
});
it('No responses with blacklisted URLs', async function () {
const blacklistedUrls = ['tia', 'example'];
page.on('response', response => {
blacklistedUrls.forEach(blacklistedUrl => {
const contains = response.url().includes(blacklistedUrl);
assert.equal(contains, false);
});
});
await page.goto('http://google.com/');
});
});
