126文字
1分
編集

Playwrightでセッションを再利用する

次のようなコマンドを入力して実行し終了すると、Cookie や Local storage を state.json に保存してくれる

jsx
playwright --save-storage=auth.json

例えば、次のようなコマンドで目的のサイトを開いてログインしてからブラウザを閉じる

jsx
npx playwright open "https://..." --save-storage=auth.json

コマンドから再利用する場合は次のようにする。

jsx
npx playwright --load-storage=auth.json

コードから再利用する場合は次のようにする。

jsx
import { test } from "@playwright/test";

test.use({
  storageState: "auth.json",
});

test("auth", async ({ page }) => {
  await page.goto("https://...");
  //...
});
編集