28. Global Authentication & Session Reuse with storageState

Save Authentication State (auth.setup.js)

import { test as setup } from "@playwright/test";

const authFile = "playwright/.auth/user.json";

setup("authenticate user", async ({ page }) => {
await page.goto("/login");
await page.getByLabel("Email").fill("tester@targetqa.com");
await page.getByLabel("Password").fill("Secret123!");
await page.getByRole("button", { name: "Sign In" }).click();
await page.waitForURL("**/dashboard");

// Save session state to disk:
await page.context().storageState({ path: authFile });
});

Task

  • Implement isValidStorageState:
  • 1. Accept state object containing cookies and origins arrays.
  • 2. Return true if state.cookies.length > 0 or state.origins.length > 0.
πŸ’‘Common doubts
Did you like the problem?
Lesson 28 of 35
JavaScript
Loading code editor…
Next