Skip to navigation

Sign in once and reuse the session

Sign in once at the start of a run, save the session, and load it in each test that needs it. Your tests then open signed-in pages directly, and the run signs in only once. This pattern works with any sign-in a test can complete, including a password, an authenticator app code, or SSO. If you don’t have one yet, start with Find a testable sign-in.

saveState writes the open site’s cookies and localStorage to a JSON file. loadState puts them back into a later test’s browser.

Before you begin

  • A test account that a test can sign in with, and its credentials in environment variables. This page uses APP_USER and APP_PASSWORD, loaded from a .env file locally and from CI secrets in CI. See Protect sign-in secrets and session files.
  • A folder for the state file that’s in .gitignore. This page uses .auth/ at the project root.
  • The selectors and text of your app’s sign-in page. Replace #user, #password, Dashboard, and Sign out in the examples with the ones your app shows.

Sign in from a beforeAll spec

Put the sign-in in its own spec and list it in beforeAll in your config. beforeAll specs finish before the rest of the run starts, so the state file exists before any test needs it.

.doc-detective.json
{
"input": "docs",
"beforeAll": "tests/sign-in.spec.json",
"loadVariables": ".env"
}
tests/sign-in.spec.json
{
"autoRecord": false,
"tests": [
{
"testId": "sign-in",
"steps": [
{ "goTo": "https://app.example.com/login" },
{ "find": { "selector": "#user", "click": true, "type": "$APP_USER" } },
{ "find": { "selector": "#password", "click": true, "type": ["$APP_PASSWORD", "$ENTER$"] } },
{ "find": "Dashboard" },
{ "saveState": { "path": "auth.json", "directory": "../.auth", "overwrite": true } }
]
}
]
}
  • The find for Dashboard waits until the app shows a signed-in page, so saveState runs after the app sets its session.
  • A relative directory resolves against the spec file by default, so this writes .auth/auth.json at the project root. Without directory, the file lands in outputDir, beside your reports.
  • overwrite replaces the file on every run, so each run starts with a fresh session. Without it, saveState fails when the file exists.
  • autoRecord: false keeps the sign-in out of recordings. See What masking doesn’t hide.

Load the session in each test

In each test, open the site, load the state, open the page again so the app reads it, and check that you’re signed in:

billing.spec.json
{
"tests": [
{
"steps": [
{ "goTo": "https://app.example.com" },
{ "loadState": { "path": "auth.json", "directory": "../.auth" } },
{ "goTo": "https://app.example.com/billing" },
{ "find": "Sign out" },
{ "screenshot": "billing.png" }
]
}
]
}

The first goTo matters. loadState applies only the cookies whose domain fits the open host, and the localStorage saved for the open origin. It fails when no site is open, or when the file has nothing for that site. The second goTo reloads the app, because most apps read their session only when a page loads.

loadState can’t tell whether the server still accepts the session, because sessions expire, and the server can revoke them. The find for an element that only a signed-in reader sees catches that. The test fails instead of taking a screenshot of the sign-in page.

This directory is relative to billing.spec.json, which sits in a folder beside .auth/. When your specs sit in different folders, set each one’s directory so it points at the same .auth/ folder:

.auth/auth.json
tests/sign-in.spec.json "directory": "../.auth"
docs/billing.spec.json "directory": "../.auth"
docs/admin/users.spec.json "directory": "../../.auth"

Load the session in a doc with inline tests

When your tests live inline in your docs, put the same steps in the page’s test comments. Readers see only the procedure. Doc Detective runs the steps in page order, together with any steps it detects from the procedure’s text:

docs/billing.md
<!-- test {"testId": "billing"} -->
<!-- step {"goTo": "https://app.example.com"} -->
<!-- step {"loadState": {"path": "auth.json", "directory": "../.auth"}} -->
<!-- step {"goTo": "https://app.example.com/billing"} -->
<!-- step {"find": "Sign out"} -->
To change your plan, open **Billing** and select **Change plan**.
<!-- step {"screenshot": "billing.png"} -->
<!-- test end -->

In .mdx files, use JSX-style comments such as {/* step {...} */}. See Inline tests for the comment forms in each file type.

Run the tests

Run Doc Detective as usual, for example npx doc-detective. The sign-in spec runs first, then the rest of your tests. A passing run shows each find for Sign out passing, and the screenshots show signed-in pages.

If the sign-in spec fails, the rest of the run still runs. Tests that load the session then fail at loadState or at the find for Sign out. Fix the sign-in first. See Troubleshoot sign-in tests.

Reuse a Playwright session

The state file uses Playwright’s storageState format. If your team’s Playwright suite already signs in and saves its state with storageState({ path }), load that file with loadState. Playwright can also read a file that saveState wrote.

A Playwright file can hold several origins. loadState applies the cookies and localStorage that fit the site you opened with goTo, and leaves the rest.

Limits

  • One site per file. The browser exposes only the open site’s cookies and storage, so one saveState saves one site. When your docs span sites, such as app.example.com and admin.example.com, goTo each one and saveState to its own file. For a sign-in that redirects through an identity provider, see Sign in through single sign-on.
  • sessionStorage isn’t saved. An app that keeps its session in sessionStorage has to sign in during each test. See The app keeps its session in sessionStorage.
  • Sessions expire. A saved session lasts as long as the app allows. Running the sign-in in beforeAll on every run keeps it fresh.
  • A sign-out ends the session for every test. A test that selects Sign out usually revokes the session on the server, so later tests that load the same file are signed out too. Give that test its own sign-in.