Instructions for Puppeteer and TypeScript
Configuring your tests with Puppeteer and TypeScript
-
In the root level of your testing folder, install the
@axe-core/watcherpackage and all of its dependencies usingnpmor your preferred package manager (for example,yarnorpnpm).npm install --save-dev @axe-core/watcher -
In your test file or files, import the
puppeteerConfig()function, thewrapPuppeteer()function, and thePuppeteerControllerclass from@axe-core/watcher/puppeteer:import { puppeteerConfig, wrapPuppeteerPage, PuppeteerController } from '@axe-core/watcher/puppeteer' -
Update your test setup code (typically in a
beforeorbeforeAllblock) by wrapping any existing code for creating abrowserinstance with a call topuppeteerConfig(), while obtaining your API key from the environment (asACCESSIBILITY_API_KEY):// Original code: browser = await puppeteer.launch({ // Your existing launch options... }) // Becomes: const ACCESSIBILITY_API_KEY = process.env.ACCESSIBILITY_API_KEY const PROJECT_ID = process.env.PROJECT_ID const browser = await puppeteer.launch( puppeteerConfig({ axe: { apiKey: ACCESSIBILITY_API_KEY, projectId: PROJECT_ID }, headless: false }) );Be sure to set
ACCESSIBILITY_API_KEYandPROJECT_IDin your environment to your personal API key (found in your axe Account, API KEYS tab) and your project ID (shown at the top of these instructions when you created your project or available from the Projects page by choosing Configure project under Settings). -
Create an instance of
PuppeteerControllerand wrap your PuppeteerbrowserContext:// Create a page instance, using your browser instance. let page = await browser.newPage() // Initialize the PuppeteerController by passing in the Puppeteer page. const controller = new PuppeteerController(page) // Use the new wrapped Puppeteer page instance. page = wrapPuppeteerPage(page, controller) -
Finally, ensure all test results from axe Watcher are flushed out. To do this, add the following block of code to your test teardown code (typically in an
afterEachblock):afterEach(async () => { await controller.flush() });
