Istruzioni per il test di componenti con Cypress e JavaScript

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

Configurare i test dei componenti con Cypress e JavaScript

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. In cypress.config.js, importa la funzione cypressConfig() dal pacchetto @axe-core/watcher e avvolgi la tua configurazione con essa. Imposta l'array testingTypes per includere component:

    const { defineConfig } = require('cypress');
    const { cypressConfig } = require('@axe-core/watcher/cypress/config');
    
    const ACCESSIBILITY_API_KEY = process.env.ACCESSIBILITY_API_KEY
    const PROJECT_ID = process.env.PROJECT_ID
    const SERVER_URL = process.env.SERVER_URL
    
    module.exports = defineConfig(  
      cypressConfig({
        axe: {
          apiKey: ACCESSIBILITY_API_KEY,
          projectId: PROJECT_ID,
          serverURL: SERVER_URL,
          testingTypes: ['component']
        },
        // Your existing Cypress configuration code here
        component: {
          // Your component testing configuration
          
          // For example:
          
          // devServer: {
          //  framework: 'react',
          //  bundler: 'webpack',
          //},
        },
      })
    );

    Assicurati di impostare ACCESSIBILITY_API_KEY e PROJECT_ID nel tuo ambiente con la tua chiave API personale (che si trova nel tuo account Axe, scheda **API KEYS**) e l'ID del tuo progetto (mostrato nella parte superiore di queste istruzioni quando hai creato il tuo progetto o disponibile nella pagina Progetti scegliendo **Configura progetto** sotto **Impostazioni**). Se la tua organizzazione utilizza un'istanza regionale, cloud privato o on-premise di Axe Developer Hub, imposta anche SERVER_URL sull'URL di base di tale istanza (per esempio, https://axe-eu.deque.com); altrimenti, ometti SERVER_URL e verrà utilizzato il https://axe.deque.com predefinito.

  3. Nel file di supporto dei componenti Cypress (comunemente chiamato cypress/support/component.js), importa il pacchetto @axe-core/watcher cypressCommands e chiama axeWatcherFlush() dopo ogni test:

    require('@axe-core/watcher/cypress/support');
    
    afterEach(() => {
      cy.axeWatcherFlush();
    });
  4. Hai finito. Esegui i test dei componenti e controlla i tuoi risultati. Si prega di notare che @axe-core/watcher funziona solo in Chrome per Testing o Chromium e dovrebbe essere utilizzato solo in modalità esecuzione di Cypress. Può essere usato con --headless=new o --headed (ad esempio, cypress run --headed --browser=chrome-for-testing --component).