Skip to navigation

Test pages behind a sign-in

Most product docs describe pages that a reader sees only after they sign in, or log in. To test those procedures or take their screenshots, your tests have to sign in too, often past multi-factor authentication (MFA). This page helps you find a sign-in that a test can complete on its own, every time. Then it helps you choose how your tests use it. Once a test is signed in, the find, click, type, and screenshot steps you use on other pages work on these pages too. For API calls that need credentials, see Authenticate API requests.

What makes a sign-in testable

A test runs with nobody watching it, so the sign-in it uses has to work without a person. A sign-in is testable when it:

  • Runs unattended. Every step is something a test can do: open a page, type, select, or compute a code.
  • Gives the same result every time. The same steps sign in the same way on every run. No prompt appears on some runs and not others.
  • Works the same locally and in CI. It doesn’t depend on a particular laptop, browser profile, or network.
  • Uses a secret you can store and rotate. A password, token, or authenticator key fits in an environment variable or a CI secret, and someone can replace it.
  • Uses a low-privilege account outside production. The account can see the pages your docs show and can’t do much harm if its secret leaks.

Ask your engineering and QA teams

Your product’s end-to-end tests already have to sign in, so your engineering or QA team has usually solved this problem. Reusing their method is often the fastest path, and it means that your docs tests break only when theirs do. Ask them:

  • How do your end-to-end tests sign in, and can docs tests use the same method?
  • Is there a staging or test tenant with seeded test users?
  • Is there a test account with MFA off? Or one with an authenticator app enrolled, whose setup key you can store as a secret?
  • Is there a test-only endpoint or API that creates a session or a token?
  • Does the non-production environment accept a fixed one-time code?
  • Does a Playwright suite already save a storageState file? loadState reads that format.
  • Who approves the test account, and who rotates its secret?

Match what your team has to a pattern

Use the answers to pick how your tests sign in:

What your team hasWhat to use in Doc Detective
A test account that signs in with a passwordSign in once in a spec that runs before the others, then reuse the session
A test account with an authenticator app enrolledGenerate the code with totp(). See Sign in with an authenticator app code
A fixed one-time code that works outside productionType it from an environment variable like a password, then reuse the session
Single sign-on (SSO) through an identity provider that has a test tenant or test usersSign in through the provider, then save the session on your app’s site. See Sign in through single sign-on
A Playwright suite that saves storageStateLoad that file with loadState. See Reuse a Playwright session
One session cookie, from a test-only endpoint or a sign-inReuse the cookie with saveCookie and loadCookie
An API token or a sign-in request that returns oneSend it in a header. See Authenticate API requests

Agree on the account with your security owner

A test account is a real account with real access, so get your security owner’s agreement on how your tests use it:

  • Use an account made for testing, in a non-production environment, with only the access your docs need.
  • Never turn off MFA on a real person’s account so that a test can sign in.
  • Never ship a sign-in bypass, such as a test-only endpoint or a fixed one-time code, to production.
  • Store the account’s secrets as CI secrets, and agree on who rotates them. See Protect sign-in secrets and session files.

Sign-ins a test can’t complete

Doc Detective doesn’t support a sign-in factor that needs a person or a second device. A test that waits for a person can’t run in CI and doesn’t give the same result twice. These factors can’t be tested:

  • Codes sent by SMS or a phone call.
  • Push approval in a mobile app.
  • Codes and magic links sent by email, which a person has to open.
  • Hardware security keys and passkeys.
  • CAPTCHA and other challenges that check for a human.

If your product’s sign-in uses one of these, don’t build the test around it. Go back to your engineering and QA teams and ask how their tests get past it. Most teams have a test account with a different factor, or a test tenant where the factor is off. Some can create a session directly. For CAPTCHA, ask whether the test environment turns it off or uses the CAPTCHA provider’s test keys.

Once a test can sign in, sign in once and reuse the result in every test. A test that signs in at the start of every procedure is slow. Every extra sign-in is also another chance to hit a rate limit or a CAPTCHA. Doc Detective can reuse a whole session or one cookie:

Session reuseCookie reuse
ActionssaveState and loadStatesaveCookie and loadCookie
What it movesEvery cookie for the open site, plus its localStorageOne cookie, by name
Where it’s storedA JSON file in Playwright’s storageState formatA Netscape cookie file, or an environment variable for the rest of the run
Keeps sameSiteYesOnly through an environment variable
Works with Playwright filesYes, in both directionsNo
Pick it whenYou don’t know how the app stores its session, or it uses several cookies or localStorageYou know the one cookie that holds the session, or you don’t want a file on disk

When you aren’t sure, use session reuse. It carries the open site’s cookies and localStorage. Start with Sign in once and reuse the session, and before you run it in CI, read Protect sign-in secrets and session files.