Playwrightと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

PlaywrightとJavaScriptを用いたテスト設定

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

    npm install --save-dev @axe-core/watcher
  2. テストファイルで、playwrightConfig()関数、wrapPlaywright()関数、およびPlaywrightControllerクラスを@axe-core/watcher/playwrightからインポートします:

    const {
      wrapPlaywrightPage,
      playwrightConfig,
      PlaywrightController
    } = require('@axe-core/watcher/playwright')
  3. テストセットアップコード(通常はbeforeまたはbeforeAllブロックで)で、browserContextインスタンスを作成する既存のコードをplaywrightConfig()を呼び出して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 = await playwright.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']
      })
    );

    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が使用されます。

  4. pageインスタンスを持ったら、PlaywrightControllerクラスのインスタンスを作成し、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. 最後に、axe Watcherからのすべてのテスト結果がフラッシュアウトされていることを確認します。これを行うには、次のコードブロックをテストのティアダウンコード(通常はafterEachブロック)に追加します:

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