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_USERandAPP_PASSWORD, loaded from a.envfile 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, andSign outin 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.
- The
findforDashboardwaits until the app shows a signed-in page, sosaveStateruns after the app sets its session. - A relative
directoryresolves against the spec file by default, so this writes.auth/auth.jsonat the project root. Withoutdirectory, the file lands inoutputDir, beside your reports. overwritereplaces the file on every run, so each run starts with a fresh session. Without it,saveStatefails when the file exists.autoRecord: falsekeeps 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:
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:
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:
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
saveStatesaves one site. When your docs span sites, such asapp.example.comandadmin.example.com,goToeach one andsaveStateto its own file. For a sign-in that redirects through an identity provider, see Sign in through single sign-on. sessionStorageisn’t saved. An app that keeps its session insessionStoragehas to sign in during each test. See The app keeps its session insessionStorage.- Sessions expire. A saved session lasts as long as the app allows. Running the sign-in in
beforeAllon 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.
Related
- Reuse one sign-in cookie, when you know the one cookie that holds the session.
- Troubleshoot sign-in tests.
- The
saveStateandloadStateschema references.