Skip to content

Lesson 5 of 9 · 24m

Seeding State, Authentication, and Storage State Correctly

Set up backend state through controlled APIs, reuse authentication safely, and avoid driving the UI for data the test does not care about.

End-to-end does not mean 'every step must happen through the browser.' It means the behavior under test is exercised through the real browser and real application boundary. If the test is about invoice refund errors, creating the invoice through seventeen pages of clicking is not rigor. It is noise, runtime, and extra failure surface.

Separate system state from behavior under test

Classify each requirement as browser-driven behavior, an API-seeded prerequisite, a fixture-owned entity, or an externally real integration. The browser-driven part should be as small as the behavior permits. Everything else belongs in fixtures or test-data APIs.

Prefer controlled test-data endpoints in nonproduction

A dedicated API for tests is not cheating when it is environment-gated and does not bypass the behavior under test:

const response = await request.post('/api/test/seed-invoice', {
  data: {
    workspaceId: workspace.id,
    amountDue: 4200,
    status: 'open',
  },
});
const invoice = await response.json();
await page.goto(`/billing/invoices/${invoice.id}`);

Guard it aggressively: disabled in production, authenticated for the test environment, audited, and incapable of touching real customer tenants. Direct database insertion is faster but couples tests to schema details and skips invariants. A test-data endpoint can use the same domain services as production while still being explicit.

Authentication setup is not the login test

Login once per worker or suite, then reuse state if the product behavior allows it:

setup('authenticate billing admin', async ({ page }) => {
  await page.goto('/login');
  await page.getByLabel('Email').fill('billing-admin@example.test');
  await page.getByLabel('Password').fill(process.env.E2E_PASSWORD!);
  await page.getByRole('button', { name: 'Sign in' }).click();
  await page.context().storageState({ path: 'playwright/.auth/billing-admin.json' });
});

A dependent project then sets use: { storageState: 'playwright/.auth/billing-admin.json' }.

Storage state captures cookies and storage as they existed at save time. It does not freeze identity server-side. If sessions expire, are revoked, or are bound to device state, your tests must model that reality.

Never share one mutable identity casually

Reusing authentication is fine for parallel tests only when the identity does not own mutable state that tests fight over. If every test updates the same user's settings, you have recreated the shared-singleton problem. The best pattern is: authenticate once to discover the session mechanism, then apply tokens or cookies to separate seeded users per worker or test.

Do not log into Gmail in CI. Capture mail at an application boundary using a test inbox service or local SMTP capture. The important property is deterministic lookup by recipient and correlation ID. If links are single-use, parallel retries and link scanning security products can consume them. Test that behavior deliberately rather than accidentally.

Field notes

  • A test-data endpoint that bypasses authorization is acceptable only when the test does not claim to validate that authorization path.
  • Do not commit real session files. Treat storage state artifacts as credentials and add them to .gitignore.
  • Seed data should be small and specific. Full production-like datasets hide the cause of failures and slow cleanup.
  • Beware caches: creating state through an API may not invalidate UI-level or edge caches the same way user workflows do.
  • If setup fails, the report should say 'fixture creation failed,' not misattribute it to the browser flow.

Was this lesson helpful?

Sign in to rate