Skip to navigation

saveState

The saveState action saves the open site’s cookies and localStorage to a JSON file. Load the file in a later test with loadState, so you sign in once instead of in every test. For the whole pattern, see Sign in once and reuse the session.

The file uses Playwright’s storageState format, so a file Playwright wrote works with loadState, and Playwright can read a file saveState wrote.

Secrets in output: A state file holds real session cookies and tokens, because loadState reads them back. It isn’t masked, and it works like a password. Add its folder (directory, or outputDir by default) to .gitignore, and leave it out of uploaded CI artifacts. See Keep secrets out of logs, reports, and captures.

For comprehensive options, see the saveState reference.

What it saves

  • Every cookie the browser exposes for the open site.
  • The open origin’s localStorage.

Open the site with goTo and sign in first. One saveState captures one site, because the browser only exposes the open site’s cookies and storage. sessionStorage isn’t saved.

Formats

String format

A path ending in .json. A relative path resolves against outputDir. It doesn’t replace an existing file.

{
"saveState": "auth.json"
}

Object format

{
"saveState": {
"path": "auth.json",
"directory": "../.auth",
"overwrite": true
}
}
FieldTypeDescriptionDefault
pathstringRequired. File to write, ending in .json. A relative path resolves against directory, or against outputDir when directory is omitted.
directorystringDirectory to save the file in. A relative directory resolves against the spec file.outputDir
overwritebooleanWhether to replace an existing file.false

Outputs

OutputDescription
pathAbsolute path of the saved file.
cookiesNumber of cookies saved.
localStorageNumber of localStorage entries saved.

Failures

MessageFix
Couldn't save state: `/abs/auth.json` exists. Set `overwrite` to `true` to replace it.Set overwrite to true, or pick another path.
Couldn't save state: no site is open. Use `goTo` first.Add a goTo to the site before saveState.

Example

{
"steps": [
{ "goTo": "https://app.example.com/login" },
{ "find": { "selector": "#user", "click": true, "type": "$USERNAME" } },
{ "find": { "selector": "#password", "click": true, "type": ["$PASSWORD", "$ENTER$"] } },
{ "saveState": { "path": "auth.json", "overwrite": true } }
]
}