クリップボードに文字列をコピーするボタンを Playwright でテストする方法をメモ。grantPermissions でクリップボードの読み書きを許可しておくのがミソ。
import { test, expect, Page } from "@playwright/test"; test("アクセスキーをコピーできる", async ({ page, context }) => { // Arrange await context.grantPermissions(["clipboard-read", "clipboard-write"]); // Act await page.getByRole("button", { name: "コピー" }).click(); // Assert const clipboardText = await page.evaluate(async () => { return await navigator.clipboard.readText(); }); await expect(clipboardText).toContain("期待する文字列"); });