Short answer
You handle two-factor login in automated tests in one of four ways: generate the TOTP code in the test from the test account’s secret, fetch the code from a captured email or SMS, log in once and save the session state for the other tests, or, carefully, use a shortcut that exists only in the test build. The best combination: 1-2 tests of the login itself with real 2FA (TOTP or a code from a message) and a saved session state for everything else. Keep secrets in CI, and test accounts exist only on the test environment.
Four ways
| approach | how it works | when | risk |
|---|---|---|---|
| TOTP code from a secret | the test computes the code from the account secret, like an authenticator app | 2FA through Google Authenticator and similar | the secret must be in CI secrets |
| code from email or SMS | the test fetches the code from Mailpit or a fake SMS gateway | 2FA by email or SMS | needs message capture on staging |
| saved session state | log in once per role, then cookies and token from a file | all tests except the login test | the session may expire during a long suite |
| shortcut in the test build | e.g. a fixed code accepted only on staging | when no other approach works | dangerous if it can be enabled in production |
Example: a TOTP code in Playwright
With the otpauth library, and the test account’s secret in an environment variable from CI secrets:
import { test, expect } from "@playwright/test";
import { TOTP } from "otpauth";
test("login with 2FA", async ({ page }) => {
const totp = new TOTP({ secret: process.env.QA_TOTP_SECRET! });
await page.goto("/login");
await page.getByLabel("Email").fill(process.env.QA_USER!);
await page.getByLabel("Password").fill(process.env.QA_PASSWORD!);
await page.getByRole("button", { name: "Log in" }).click();
await page.getByLabel("Code from app").fill(totp.generate());
await page.getByRole("button", { name: "Confirm" }).click();
await expect(page.getByRole("heading", { name: "Dashboard" })).toBeVisible();
await page.context().storageState({ path: ".auth/user.json" });
});
The TOTP secret is the value the app shows when you enable 2FA, usually as a QR code. For a test account, save it once, when creating the account.
Saved session state for the other tests
In playwright.config.ts, set up a project that prepares the session and reuse it in the other tests:
projects: [
{ name: "setup", testMatch: /auth\.setup\.ts/ },
{
name: "e2e",
dependencies: ["setup"],
use: { storageState: ".auth/user.json" },
},
],
The 2FA login runs once per run, not in every test. Add the .auth/ folder to .gitignore.
What not to do
- don’t turn off 2FA for the whole environment: then the 2FA login isn’t tested at all;
- don’t keep secrets and passwords in the repository: only in CI secrets;
- don’t use test accounts in production with real data;
- don’t add shortcuts controlled by a flag or variable that can be switched on in production;
- don’t log in with 2FA in every test: it’s slow, and with SMS codes you may hit the provider’s limits.
Fetching codes from emails and SMS is covered in how to test emails and SMS in automated tests, and secrets in CI in Playwright tests in GitHub Actions and GitLab CI.
How I can help
As part of test automation I set up login in E2E tests, including with 2FA, with secrets in CI and a saved session state for each role. Describe how login works in your app in the contact form.