I've got a synthetic monitoring test that has been running fine. I've added some code to block third-party ad tracking and analytic scripts to speed up the tests (and not skew analytics), as recommended here: https://scrapingant.com/blog/block-requests-playwright.
At the top of each test I've got this:
await page.route('**/*', filterResources)
And the filterResources method is defined as follows:
const DOMAIN_EXCLUSIONS = [
'connect.facebook.net',
'px.ads.linkedin.com',
[...]
]
module.exports = (route) => {
let matches = DOMAIN_EXCLUSIONS.filter((domain) => {
return route.request().url().startsWith('https://' + domain)
})
return matches.length ? route.abort() : route.continue()
}
Now occasionally the following error will be thrown, and the test fails:
route.continue: Target page, context or browser has been closed
This maybe happens in one every 6-8 runs.
Is this because the page navigates away while some requests are still outstanding, because Playwright believes the page is already fully loaded? I'm using the proper awaits and the test was working fine before.
I'm catching and solving the promise now, and the issue doesn't seem to happen any more:
if (matches.length) {
return route.abort()
}
return route.continue().catch(() => {})