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://...");
//...
});