> For clean Markdown of any page, append .md to the page URL.
> For a complete documentation index, see https://docs.doc-detective.com/llms.txt.
> For AI client integration (Claude Code, Cursor, etc.), connect to the MCP server at https://docs.doc-detective.com/_mcp/server.

# Reuse one sign-in cookie

> Save the one cookie that holds your app's session with saveCookie, and load it in later tests with loadCookie, through a file or an environment variable.

When your app keeps its whole session in one cookie, you can sign in once and move only that cookie into each test. [`saveCookie`](/docs/actions/savecookie) saves one cookie, by name, to a file or an environment variable. [`loadCookie`](/docs/actions/loadcookie) sets it in a later test's browser.

Cookie reuse is separate from [session reuse](/docs/test-docs/reuse-a-session), which saves every cookie and `localStorage` for the open site. Use cookie reuse when you know which cookie holds the session, or when you don't want a file on disk. When you aren't sure, use session reuse. See [Choose between session reuse and cookie reuse](/docs/test-docs/sign-in#choose-between-session-reuse-and-cookie-reuse).

## Before you begin

* A sign-in a test can complete. See [Find a testable sign-in](/docs/test-docs/sign-in).
* The name of the session cookie, such as `session_id`. Ask your engineering team, or sign in with a browser and look at the site's cookies in its developer tools.
* The test account's credentials in environment variables. This page uses `APP_USER` and `APP_PASSWORD`.

## Reuse the cookie through a file

A file works across specs and across runs. `saveCookie` writes the cookie in Netscape cookie format, the format curl reads, and `loadCookie` reads it back.

1. Sign in from a `beforeAll` spec, and save the cookie after the app shows a signed-in page:

   **`tests/sign-in.spec.json`**

   ```json title="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" },
           {
             "saveCookie": {
               "name": "session_id",
               "path": "session.txt",
               "directory": "../.auth",
               "overwrite": true
             }
           }
         ]
       }
     ]
   }
   ```

   List the spec in `beforeAll` in your config, as in [Sign in from a `beforeAll` spec](/docs/test-docs/reuse-a-session#sign-in-from-a-beforeall-spec). A relative `directory` resolves against the spec file by default, so this writes `.auth/session.txt` at the project root.

2. In each test, open the site, load the cookie, and open the page again so the app reads it:

   **`billing.spec.json`**

   ```json title="billing.spec.json"
   {
     "tests": [
       {
         "steps": [
           { "goTo": "https://app.example.com" },
           { "loadCookie": { "name": "session_id", "path": "session.txt", "directory": "../.auth" } },
           { "goTo": "https://app.example.com/billing" },
           { "find": "Sign out" },
           { "screenshot": "billing.png" }
         ]
       }
     ]
   }
   ```

   `loadCookie` sets the cookie only when its domain fits the open page, so the first `goTo` has to open that site. The `find` for `Sign out` fails the test when the server no longer accepts the session.

The string form is a shorthand. `{ "saveCookie": "session_id.txt" }` saves the cookie named `session_id` to `session_id.txt` in `outputDir`, and `{ "loadCookie": "session_id.txt" }` loads it from there. Use the object form when you need `directory`, `overwrite`, or `domain`.

## Reuse the cookie through an environment variable

An environment variable keeps the cookie off the disk. `saveCookie` stores the cookie as JSON in the variable. The variable lasts for the rest of the run, so tests in later specs can load it. It doesn't carry over to the next run.

1. In the `beforeAll` sign-in spec, save the cookie to a variable:

   **`tests/sign-in.spec.json`**

   ```json title="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" },
           { "saveCookie": { "name": "session_id", "variable": "SESSION_COOKIE" } }
         ]
       }
     ]
   }
   ```

2. In each test, load the cookie from the variable:

   **`billing.spec.json`**

   ```json title="billing.spec.json"
   {
     "tests": [
       {
         "steps": [
           { "goTo": "https://app.example.com" },
           { "loadCookie": { "name": "session_id", "variable": "SESSION_COOKIE" } },
           { "goTo": "https://app.example.com/billing" },
           { "find": "Sign out" },
           { "screenshot": "billing.png" }
         ]
       }
     ]
   }
   ```

A step sets either `variable` or `path`, not both, and `directory` works only with `path`.

The string form saves to a variable with the cookie's own name: `{ "saveCookie": "session_id" }` saves the cookie `session_id` to the variable `session_id`, and `{ "loadCookie": "session_id" }` loads it.

## Pick the right cookie with `domain`

A browser can hold two cookies with the same name for different domains, such as `example.com` and `app.example.com`. Without `domain`, `saveCookie` saves the first cookie with that name. Set `domain` to pick one:

```json
{ "saveCookie": { "name": "session_id", "path": "session.txt", "directory": "../.auth", "domain": "app.example.com" } }
```

`domain` matches that domain and its subdomains. On `loadCookie`, it picks the cookie from the file in the same way. Either way, the cookie's own domain still has to fit the page you opened: the same host, or a parent domain of it. On `localhost` and private network addresses, Doc Detective leaves the domain to the browser.

## Replace the file with `overwrite`

`saveCookie` doesn't replace an existing file unless you set `"overwrite": true`, and fails instead. Set it in a `beforeAll` sign-in, so each run saves a fresh cookie. Saving to a variable always replaces the variable's value.

## Keep `httpOnly` and `sameSite`

* **`httpOnly`.** Session cookies are often `httpOnly`, which hides them from the page's own scripts. `saveCookie` reads them anyway, and both forms keep the flag, so `loadCookie` sets the cookie as `httpOnly` again.
* **`sameSite`.** A variable keeps the cookie's `sameSite` value. A cookie file has no standard place for it, so a cookie loaded from a file gets `Lax`. If your app needs `Strict` or `None`, such as for a sign-in used inside another site, use a variable or [session reuse](/docs/test-docs/reuse-a-session). A `None` cookie also needs HTTPS: on an `http://` page, `loadCookie` sets it as `Lax`.

A cookie keeps its expiry. `loadCookie` fails when the cookie has already expired, so sign in again on each run rather than keeping an old cookie file.

## Keep the cookie secret

A cookie file holds the real session value, because `loadCookie` reads it back. Add its folder to `.gitignore`, and leave it out of uploaded CI artifacts. Logs and reports don't mask a cookie saved to a variable until a later step uses it as `$NAME`. Don't print the variable in `runShell` or `runCode` commands. See [Protect sign-in secrets and session files](/docs/test-docs/sign-in-secrets).

## Related

* [Troubleshoot sign-in tests](/docs/test-docs/sign-in-troubleshooting).
* The [`saveCookie`](/reference/schemas/savecookie) and [`loadCookie`](/reference/schemas/loadcookie) schema references.