axe-core-npm
axe-core-npm copied to clipboard
Uncatchable exception occurs in analyze function
Product
playwright
Product Version
4.7.3
Latest Version
- [X] I have tested the issue with the latest version of the product
Issue Description
Expectation
I expect that if I wrap the call to the analyze function in a try catch statement or use a promise.catch, I would be able to catch any exception that is thrown by the function.
Actual
There are certain cases that can cause an uncatchable exception to be thrown by the function.
How to Reproduce
import AxeBuilder from "@axe-core/playwright";
import playwright = require("playwright");
process.on("uncaughtException", (err) => {
console.error("An uncaught exception occurred: ", err);
switch (err.message) {
case "page.evaluate: Execution context was destroyed, most likely because of a navigation":
console.error(
"The page execution context was destroyed. Attempting to continue..."
);
break;
default:
process.exit(1);
}
});
(async () => {
const browser = await playwright.chromium.launch({ headless: false });
const context = await browser.newContext();
const page = await context.newPage();
console.info(`Loading page...`);
page.goto("https://youtube.com");
try {
console.info(`Analyzing page...`);
await new AxeBuilder({ page }).analyze().catch((e) => {
console.error(`Error occurred when analyzing page: ${e}`);
});
} catch (e) {
console.error(`Error occurred when analyzing page: ${e}`);
}
console.info(`Loading page new page...`);
await page.goto("https://google.com");
await browser.close();
})();
Example output
Debugger attached.
Loading page...
Analyzing page...
An uncaught exception occurred: page.evaluate: Execution context was destroyed, most likely because of a navigation
at AxeBuilder.analyze (/Users/nscribano/Developer/go/src/bitbucket.org/atlassian/axe-scanner/node_modules/@axe-core/playwright/dist/index.js:236:10)
at /Users/nscribano/Developer/go/src/bitbucket.org/atlassian/axe-scanner/dist/src/index.js:26:50 {
name: 'Error'
}
The page execution context was destroyed. Attempting to continue...
Error occurred when analyzing page: Error: page.evaluate: Execution context was destroyed, most likely because of a navigation
Loading page new page...
Waiting for the debugger to disconnect...
Additional context
The "how to reproduce" code is a bit convoluted because the exception would not occur if I were to await the page.goto("https://youtube.com"); call. However, I have seen this bug happen a handful of times in a real environment where I have a service that is iterating across URLs and scanning them with axe-core/playwright. That bug is difficult to recreate consistently, but the code I've included above has the same effect. Either way, I would expect to be able to catch the exception that happens.
Thanks for the issue. We'll try to update the code to better handle this scenario.