Injecting Axe a11y tester
I would like to use Crawlee to crawl a bunch of internal sites and run the Axe accessibility scanner on each page. I figured out how to inject the script they reference in their getting started docs (https://github.com/dequelabs/axe-core#getting-started) using the
page.addInitScript
.
But every page after that throws this error.
INFO PlaywrightCrawler: Starting the crawl
INFO PlaywrightCrawler: Title of https://dequeuniversity.com/demo/mars/ is 'Mars Commuter: Travel to Mars for Work or Pleasure!'
WARN PlaywrightCrawler: Reclaiming failed request back to the list or queue. Required "window" or "document" globals not defined and cannot be deduced from the context. Either set the globals before running or pass in a valid Element. {"id":"9syPc5JbUuAjPx1","url":"https://dequeuniversity.com/demo/mars/","retryCount":1}
If I comment out the call to axe.run() the error goes away and things 'work'.
Any idea what could be causing this?Mars Commuter: Travel to Mars for Work or Pleasure!
MarsCommuter - Your gateway to the Red Planet
GitHub
GitHub - dequelabs/axe-core: Accessibility engine for automated Web...
Accessibility engine for automated Web UI testing. Contribute to dequelabs/axe-core development by creating an account on GitHub.
8 Replies
fair-roseOP•3y ago
My code was wrong. This should be a better example
Ahh, and I needed to add a try/catch to log the error that was being hidden from stdout. Nevermind, nothing to see here :S
absent-sapphire•3y ago
I would not recommend putting the
page.addInitScript
in the requestHandler
because requestHandler
runs once the load
event has fired. Try adding that into preNavigationHooks
instead like this:
The error you're receiving is because the axe script was never run on the page since it had already been initialized.
If the above doesn't work, you can try this hacky solution I used a while back to run an init script. This registers the init script at the browser level to ensure it is ALWAYS run before any other scripts on the page:
Second one should work 100%
Hope this helps!fair-roseOP•3y ago
I tried the second one and after wrapping the call to
axe.run
in a try/catch I get this error.
Error: Required "window" or "document" globals not defined and cannot be deduced from the context. Either set the globals before running or pass in a valid Element.
at setupGlobals (/Users/LGoolsby/dev/playground/axe/sample/node_modules/axe-core/axe.js:21231:15)
at Object.run4 [as run] (/Users/LGoolsby/dev/playground/axe/sample/node_modules/axe-core/axe.js:21486:7)
at PlaywrightCrawler.requestHandler [as userProvidedRequestHandler] (file:///Users/LGoolsby/dev/playground/axe/sample/src/main.ts:43:39)
at async wrap (/Users/LGoolsby/dev/playground/axe/sample/node_modules/@apify/src/index.ts:77:27)@Lane Goolsby just advanced to level 1! Thanks for your contributions! 🎉
fair-roseOP•3y ago
Same error with the first one as well
The line in the axe.js that's blowing chunks is doing this:
fair-roseOP•3y ago
This seems relevant https://github.com/apify/crawlee/pull/1337
GitHub
feat: add
playwright.utils.injectJQuery
by barjin · Pull Request ...closes #1336
JQuery requires the global document object, which is not available when Page.addInitScript-s are added. This solution, therefore, introduces the new injectFile option waitForDOM, which...
fair-roseOP•3y ago
I figured it out! Here's a working solution.
absent-sapphire•3y ago
Super nice!! Running the script after the page has loaded seems to also work