Storage state: start from a prepared session
Why This Matters
Many tests need the same starting point: a logged-in user, a filled cart, accepted cookies. Clicking there through the UI in every test is slow, and every click is one more thing that can fail for a reason that has nothing to do with the test. Playwright can save the state of a browser session once and start any number of tests from it.
What storage state is
storageState is a JSON file with a browser context’s cookies and local storage. Save it once:
await page.context().storageState({ path: "playwright/.auth/shop-with-cart.json" });
and start a new context from it:
use: { storageState: "playwright/.auth/shop-with-cart.json" }
That is all it stores. Anything the application keeps only in memory is gone after a page load, and storage state cannot bring it back.
What that means on the practice site
The two parts of the site behave differently, which makes them a good illustration:
- The shop keeps your sandbox in a
shop_sessioncookie (the API documentation says so). Save the storage state and you save the cart along with it. - The login page keeps nothing: it is a practice form that checks the practice accounts in the page itself and forgets everything on reload. There is no login session to save.
On a real application with a real login, the session cookie or token is exactly what you save; the pattern below is the same.
A setup project
The repository saves the state in a setup project, which runs before the tests that need it. tests/module-4/shop-session.setup.ts:
import { expect, test as setup } from "@playwright/test";
const stateFile = "playwright/.auth/shop-with-cart.json";
setup("prepare a session with one product in the cart", async ({ page }) => {
await page.goto("/shopping");
await expect(page.getByTestId("products-loading")).toBeHidden();
await page.getByTestId(/^product-\d+$/).filter({ hasText: "Bluetooth Headphones" })
.getByRole("button", { name: "Add to Cart" }).click();
await expect(page.getByTestId("cart-button")).toContainText("1 items");
await page.context().storageState({ path: stateFile });
});
And in playwright.config.ts:
{ name: "setup", testMatch: /\.setup\.ts$/, use: { ...devices["Desktop Chrome"] } },
{
name: "with-cart",
testMatch: /with-cart\.spec\.ts$/,
dependencies: ["setup"],
use: { ...devices["Desktop Chrome"], storageState: "playwright/.auth/shop-with-cart.json" },
},
dependencies: ["setup"] makes Playwright run the setup project first. Every test in with-cart then opens a browser that already has the session:
test("starts with the prepared cart", async ({ page }) => {
await page.goto("/shopping");
await expect(page.getByTestId("cart-button")).toContainText("1 items");
});
npx playwright test --project=with-cart
playwright/.auth/ is in .gitignore: a saved session is a credential on a real application and never belongs in git.
The catch: shared state
Every test that starts from the file shares the same session. On the shop that means the same sandbox: if one test removes the headphones while another checks for them, the result depends on which runs first. Use saved state for a starting point that tests read, not for data they change. Tests that change data get their own sandbox, as the API tests in this module do.
Exercise
- Run
npx playwright test --project=with-cartand openplaywright/.auth/shop-with-cart.json. Which cookie carries the cart? - Add a test to
with-cart.spec.tsthat removes the headphones from the cart. Run the project a few times with--repeat-each=3. What happens to starts with the prepared cart?
Show suggested answer
- The
shop_sessioncookie. Its value is the sandbox id; the cart itself stays on the server, in that sandbox. - The tests start interfering with each other. Depending on the order, starts with the prepared cart sometimes finds 0 items, because another test in the same sandbox removed them. That is the shared-state trap. Keep changing tests out of the shared session, or give them a sandbox of their own (an API fixture, or a fresh browser context without the saved state).