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_USERandAPP_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.
-
Sign in from a
beforeAllspec, and save the cookie after the app shows a signed-in page:tests/sign-in.spec.jsonList the spec in
beforeAllin your config, as in Sign in from abeforeAllspec. A relativedirectoryresolves against the spec file by default, so this writes.auth/session.txtat the project root. -
In each test, open the site, load the cookie, and open the page again so the app reads it:
billing.spec.jsonloadCookiesets the cookie only when its domain fits the open page, so the firstgoTohas to open that site. ThefindforSign outfails 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.
-
In the
beforeAllsign-in spec, save the cookie to a variable:tests/sign-in.spec.json -
In each test, load the cookie from the variable:
billing.spec.json
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:
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 oftenhttpOnly, which hides them from the page’s own scripts.saveCookiereads them anyway, and both forms keep the flag, soloadCookiesets the cookie ashttpOnlyagain.sameSite. A variable keeps the cookie’ssameSitevalue. A cookie file has no standard place for it, so a cookie loaded from a file getsLax. If your app needsStrictorNone, such as for a sign-in used inside another site, use a variable or session reuse. ANonecookie also needs HTTPS: on anhttp://page,loadCookiesets it asLax.
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.
Related
- Troubleshoot sign-in tests.
- The
saveCookieandloadCookieschema references.