Cypressコンポーネントテストと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

CypressとJavaScriptを用いたコンポーネントテストの設定

Not for use with personal data
  1. テストフォルダのルートレベルで、@axe-core/watcherパッケージとそのすべての依存関係をnpmまたはお好みのパッケージマネージャー(例えば、yarnpnpm)を使用してインストールします。

    npm install --save-dev @axe-core/watcher
  2. cypress.config.jsで、@axe-core/watcherパッケージからcypressConfig()関数をインポートし、それで設定をラップします。testingTypes配列には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',
          //},
        },
      })
    );

    ACCESSIBILITY_API_KEYPROJECT_ID を環境に設定し、個人用 API キー(Axe アカウントの**API KEYS**タブで見つかります)とプロジェクト ID(プロジェクトを作成した際にこれらの指示の上部に表示されるか、プロジェクトページで**プロジェクトを設定**を選択して**設定**から入手できます)を使用してください。組織が地域のプライベートクラウドやオンプレミスの Axe Developer Hub インスタンスを使用している場合は、そのインスタンスのベース URL をSERVER_URLに設定してください(例: https://axe-eu.deque.com)。それ以外の場合、SERVER_URLを省略し、デフォルトのhttps://axe.deque.comが使用されます。

  3. Cypressコンポーネントサポートファイル(一般的にcypress/support/component.jsと呼ばれます)で、@axe-core/watcherパッケージをcypressCommandsインポートし、各テスト後にaxeWatcherFlush()を呼び出します:

    require('@axe-core/watcher/cypress/support');
    
    afterEach(() => {
      cy.axeWatcherFlush();
    });
  4. これで完了です。コンポーネントテストを実行し、結果を確認してください。@axe-core/watcherはChrome for TestingまたはChromiumでのみ実行され、Cypressの実行モードでのみ使用する必要があります。これは--headless=newまたは--headed(例:cypress run --headed --browser=chrome-for-testing --component)と共に使用可能です。