Java と Playwright の手順

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

Java と Playwright でテストを設定する

Not for use with personal data
  1. Java Watcher パッケージの新しい依存関係を pom.xml(Maven 用)依存関係セクションに追加します(Java Watcher パッケージは Maven Central で利用可能です)。

    <dependencies>
    
    <!-- Add this dependency: -->
      <dependency>
         <groupId>com.deque.axe_core</groupId>
         <artifactId>watcher</artifactId>
         <version>4.4.0</version> <!-- Update this as needed -->
      </dependency>
    
    </dependencies>
  2. Java Watcher のインポートをテストコードに追加します。

    import com.deque.axe_core.commons.AxeWatcherOptions;
    import com.deque.axe_core.playwright.AxeWatcherPlaywright;
    import com.deque.axe_core.playwright.AxeWatcherPage;
  3. 個人の API キーとプロジェクト ID(どちらもセキュリティと柔軟性のために環境に保存されています)を含む Java Watcher のセットアップコードを追加します。

    AxeWatcherOptions options =
        new AxeWatcherOptions()
            .setApiKey(System.getenv("ACCESSIBILITY_API_KEY"))
            .setProjectId(System.getenv("PROJECT_ID"));
    // Optional: uncomment and set SERVER_URL if using a regional, private cloud, or on-premises instance:
    // options.setServerUrl(System.getenv("SERVER_URL"));
    AxeWatcherPlaywright watcher = new AxeWatcherPlaywright(options);
    BrowserType.LaunchPersistentContextOptions launchOptions =
        watcher.configure(new BrowserType.LaunchPersistentContextOptions());
    // Watcher requires a persistent context. You must set headless explicitly on Playwright 1.49+:
    launchOptions.setHeadless(false);
    BrowserContext context = browserType.launchPersistentContext(userDataDir, launchOptions);
    AxeWatcherPage page = watcher.wrapPage(context.newPage());
    important

    Watcher には Playwright 永続的コンテキスト が必要です。非永続的なBrowserランチとは互換性がありません。Playwright Java 1.49 以降では、setHeadless(false)(または setChannel("chrome")setChannel("chromium") を使用したsetHeadless(true))を明示的に呼び出す必要があります。デフォルトのchromium-headless-shellモードはサポートされていません。

    このコードスニペットは、アクセシビリティテストを有効にした状態で Playwright Page をラップするAxeWatcherPageインスタンスを作成します。

    違反が見つかった場合にスクリーンショットをキャプチャするには、AxeWatcherOptions.setTakeScreenshots(true)を追加します。また、スクリーンショットをローカルに保存するには、.setScreenshotDir("./axe-screenshots")をチェーンします。詳細については違反スクリーンショットのキャプチャを参照してください。

    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. テストセッションの終了時にflush()を呼び出します。

    page.axeWatcher().flush();

    flush()を呼び出すことでテストランが終了したことを示し、結果をユーザーに処理・提示することができます。

    違反を特定のテストに関連付けるには、テストロジックの前にsetTestContext()を呼び出します。

    page.axeWatcher().setTestContext(
        "src/test/java/com/acme/LoginTest.java",
        java.util.Arrays.asList("LoginTest", "rejects bad password")
    );

    詳細についてはテストへの違反の帰属を参照してください。