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)と共に使用可能です。