Skip to navigation

Reuse one sign-in cookie

When your app keeps its whole session in one cookie, you can sign in once and move only that cookie into each test. saveCookie saves one cookie, by name, to a file or an environment variable. loadCookie sets it in a later test’s browser.

Cookie reuse is separate from session reuse, 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.

Before you begin

  • A sign-in a test can complete. See Find a testable 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.

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
    {
    "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. 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
    {
    "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.

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
    {
    "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
    {
    "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.

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:

{ "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. 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.

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.