Skip to navigation

saveCookie

The saveCookie action saves a browser cookie to a file or environment variable for later reuse. This action is useful for preserving authentication sessions, user preferences, or other stateful information between test runs or across different tests.

Reload saved cookies with the loadCookie action.

Secrets in output: A cookie file holds the real cookie value, because loadCookie reads it back. Unlike the output files that actions such as runShell, runCode, httpRequest, and runBrowserScript save with path, it isn’t masked. An authentication cookie works like a password. Add the cookie file’s folder (directory, or outputDir by default) to .gitignore, and leave it out of uploaded CI artifacts.

A cookie saved with variable holds the real value too. Saving it doesn’t register it for masking, even when the variable’s name looks secret. Logs and reports mask it only after a later step substitutes it as $NAME in a step field. That field can be a URL, a selector, or a header value. Loading the cookie with loadCookie doesn’t register it for masking.

A runShell or runCode command that reads the cookie variable from its environment prints the real cookie. Don’t print the variable or list the environment in those commands. See Keep secrets out of logs, reports, and captures.

For comprehensive options, see the saveCookie reference.

Use cases

  • Session persistence: Save authentication cookies to maintain login state across test runs
  • Multi-step workflows: Preserve cookies between different test specifications
  • Cross-browser testing: Transfer cookies between different browser contexts
  • CI/CD integration: Store authentication state as environment variables for automated testing
  • Test data management: Save cookies to files for debugging and test data inspection

Formats

The saveCookie action supports multiple formats:

String format

A cookie name saves that cookie to the environment variable of the same name. A path ending in .txt saves the cookie named for the file, session_token here, to that path in outputDir:

Save to environment variable
{
"saveCookie": "session_token"
}
Save to file
{
"saveCookie": "session_token.txt"
}

Object format

Save a cookie with detailed configuration:

{
"saveCookie": {
"name": "auth_cookie",
"path": "auth-session.txt",
"directory": "./test-data",
"overwrite": true
}
}

Configuration options

FieldTypeDescriptionDefault
namestringRequired. Name of the specific cookie to save.
variablestringRequired if path isn’t set. Environment variable name to store the cookie as JSON string. Can’t be used with path.
pathstringRequired if variable isn’t set. File path to save the cookie, in Netscape cookie format. A relative path resolves against directory, or against outputDir when directory is omitted. Can’t be used with variable.
directorystringDirectory to save the cookie file in. A relative directory resolves against the spec file.outputDir
overwritebooleanWhether to overwrite an existing cookie file.false
domainstringSpecific domain to filter the cookie by (optional).

The object form requires name plus exactly one of path or variable. Set path to write a cookie file, or variable to store the cookie in an environment variable, but not both. Omitting both fails validation.

overwrite here is a plain boolean (true or false). This differs from the regression-testing actions, where overwrite is the string enum "true", "false", or "aboveVariation". Those actions are screenshot, record, httpRequest, runCode, runShell, and runBrowserScript.

Examples

The docs tests run every example below except the specific-file example.

Save to default location

{
"steps": [
{
"description": "Navigate to login page and authenticate",
"goTo": "https://example.com/login"
},
{
"description": "Fill in login credentials",
"find": {
"elementText": "Username",
"type": "testuser"
}
},
{
"description": "Save the session cookie after login",
"saveCookie": "session_token"
}
]
}

Save to specific file

{
"steps": [
{
"description": "Save authentication cookie to specific file",
"saveCookie": {
"name": "auth_cookie",
"path": "auth-session.txt",
"directory": "./test-data",
"overwrite": true
}
}
]
}

Save to environment variable

{
"steps": [
{
"description": "Save session cookie to environment variable",
"saveCookie": {
"name": "session_token",
"variable": "SESSION_TOKEN"
}
}
]
}

Save with domain filtering

{
"steps": [
{
"description": "Save cookie for specific domain",
"saveCookie": {
"name": "login_token",
"path": "login-token.txt",
"domain": "app.example.com"
}
}
]
}