Istruzioni per Playwright e TypeScript

This page is not available in the language you requested. You have been redirected to the English version of the page.
Link to this page copied to clipboard

Configurazione dei test con Playwright e TypeScript

Not for use with personal data
  1. Nella cartella principale del tuo ambiente di test, installa il pacchetto @axe-core/watcher e tutte le sue dipendenze utilizzando npm o il tuo gestore di pacchetti preferito (ad esempio, yarn o pnpm).

    npm install --save-dev @axe-core/watcher
  2. Nel tuo file o nei tuoi file di test, importa la funzione playwrightConfig(), la funzione wrapPlaywright() e la classe PlaywrightController da @axe-core/watcher/playwright:

    import {
      wrapPlaywrightPage,
      playwrightConfig,
      PlaywrightController
    } from '@axe-core/watcher/playwright';
  3. Nel codice di configurazione del test (tipicamente in un blocco before o beforeAll), avvolgi qualsiasi codice esistente per creare un'istanza browserContext con una chiamata a playwrightConfig() fornendo la tua chiave API:

    
    const ACCESSIBILITY_API_KEY = process.env.ACCESSIBILITY_API_KEY
    const PROJECT_ID = process.env.PROJECT_ID
    const SERVER_URL = process.env.SERVER_URL
    
    const browserContext: BrowserContext = await chromium.launchPersistentContext(
      /**
       * We set userDataDir to an empty string which saves
       * browser data to a temporary directory
       * @see https://playwright.dev/docs/api/class-browsertype#browser-type-launch-persistent-context
       */
      '',
      playwrightConfig({
        axe: {
          apiKey: ACCESSIBILITY_API_KEY,
          projectId: PROJECT_ID,
          serverURL: SERVER_URL
        },
        /**
         * We support both headless and headed mode with Chrome.
         * 
         * For headless mode:
         * - Use '--headless=chrome' for browsers v94-108.
         * - Use '--headless=new' for browsers v109+.
         * 
         * For headed mode:
         * - Remove the '--headless' flag.
         * 
         * @see https://playwright.dev/docs/chrome-extensions#headless-mode
         */
        headless: false,
        args: ['--headless=new']
      })
    );

    Assicurati di impostare ACCESSIBILITY_API_KEY e PROJECT_ID nel tuo ambiente con la tua chiave API personale (trovata nel tuo account axe, nella scheda **API KEYS**) e con il tuo ID progetto (mostrato in cima a queste istruzioni quando hai creato il tuo progetto o disponibile nella pagina Progetti selezionando **Configura progetto** sotto **Impostazioni**). Se la tua organizzazione utilizza una versione regionale, cloud privata o un'istanza Axe Developer Hub on-premises, imposta anche SERVER_URL all'URL base di quella istanza (ad esempio, https://axe-eu.deque.com); altrimenti, ometti SERVER_URL e verrà utilizzato il valore predefinito https://axe.deque.com.

  4. Una volta che hai un'istanza di page, crea un'istanza della classe PlaywrightController e avvolgi il tuo browserContext:

    // Create a page instance, using your browser context.
    let page = await browserContext.newPage();
    
    // Initialize the PlaywrightController by passing in the Playwright page.
    const controller = new PlaywrightController(page);
    
    // Use the new wrapped Playwright page instance.
    page = wrapPlaywrightPage(page, controller);
  5. Infine, assicurati che tutti i risultati di test di axe Watcher siano scaricati. Per fare ciò, aggiungi il seguente blocco di codice al tuo codice di smantellamento del test (tipicamente in un blocco afterEach):

    afterEach(async () => {
      await controller.flush()
    })