Test Auth0 TOTP MFA with Playwright
Automate Auth0 authenticator enrollment with Playwright and MailSlurp, submit a fresh TOTP code, and verify the signed-in user's identity.
A browser test for multi-factor authentication needs to do more than fill a password. It must enroll or use the right authenticator, submit a current code, and check which user reached the protected application. Copying a code from a phone makes that difficult to repeat in CI.
MailSlurp provides test inboxes and virtual TOTP authenticators that Playwright can control through an API. For a test account you own, pair a virtual device with Auth0's enrollment secret and request a fresh code when the browser reaches the challenge. Auth0 still checks that code before returning the user to your application.
This example covers first-time signup, authenticator enrollment, and the resulting authenticated session. Email ownership verification and a later login by an already-enrolled account are separate checks; neither is established simply by completing enrollment.
What we are testing
The sample Vue application uses Auth0 for login. Its home page has a Login button, and its profile route requires authentication.

A new user enters an email address and password, pairs an authenticator when Auth0 prompts them, then supplies its one-time code. The test follows that journey and finishes by checking the profile email against the inbox created for this run.
The repository folder also contains other test integrations. The relevant source is playwright/mfa-totp.spec.ts. Start from that file and add the profile assertion below; its original final action accepts the application's consent prompt.
Setting up
Use a dedicated Auth0 test tenant and application. Configure the Vue sample's auth_config.json for your application, allow its local origin and callback URL in Auth0, and start it at http://localhost:3000. Enable the One-time Password factor and an MFA policy that challenges this test user, following Auth0's MFA setup.
From the sample project directory, install its dependencies and Chromium, then start the app:
npm install
npx playwright install chromium
npm run start
The sample includes @playwright/test and mailslurp-client. In the terminal or CI job that runs Playwright, set API_KEY to your MailSlurp API key using your secret settings. That variable is the name read by the sample; keep the key out of source control and browser code.
Use fresh test identities for enrollment. If your tenant also requires email verification before proceeding, complete that requirement with a separate email verification test appropriate to your app. The steps here do not click an Auth0 verification email or assert an email_verified claim.
Write the enrollment test
Keep these operations in the same Playwright test, using the sample's page fixture and APP_URL. The complete source linked above supplies that wrapper. Its imports include test and expect from @playwright/test; expect is also needed for the final account assertion.
Load the application
Open the Vue app, click Login, and choose Sign up in Auth0:
await page.goto(APP_URL);
await page.getByRole('button', { name: 'Login' }).click();
await page.getByRole('link', { name: 'Sign up' }).click();
The supplied example uses the English Auth0 form. Match the accessible labels in your tenant if you customize Universal Login or use another language.

Sign up with email
Import MailSlurp and create a new inbox. Its address identifies this account throughout the test, including the assertion at the end.
import { MailSlurp } from 'mailslurp-client';
const apiKey = process.env.API_KEY;
if (!apiKey) throw new Error("Set API_KEY for the MailSlurp test account");
const mailslurp = new MailSlurp({ apiKey });
const inbox = await mailslurp.createInbox();
const tempEmail = inbox.emailAddress;
Fill the form with that address and a password supplied by your test environment. This replaces the sample's fixed demonstration password and avoids revealing it through the form's Show password control.
const password = process.env.TEST_PASSWORD;
if (!password) throw new Error("Set TEST_PASSWORD for the test account");
await page.getByRole("textbox", { name: "Email address" }).fill(tempEmail);
await page.getByRole("textbox", { name: "Password" }).fill(password);
await page.getByRole("button", { name: "Continue" }).click();
Create a virtual authenticator
TOTP codes are generated from a shared secret and the current time. During first-time enrollment, Auth0 offers a QR code for an authenticator app and a manual setup secret. The sample's Trouble Scanning? and Copy code controls expose and select that secret, so Playwright can read the selected text.
Replace the sample's fixed delay with a check that the selected value is a Base32 secret before using it:
await page.getByRole("button", { name: "Trouble Scanning?" }).click();
await page.getByRole("button", { name: "Copy code" }).click();
let secret = "";
await expect.poll(async () => {
secret = await page.evaluate(() => window.getSelection()?.toString() || "");
return /^[A-Z2-7]+=*$/i.test(secret);
}).toBe(true);
This reads the text selected by that version of the form, not the system clipboard. If your Auth0 form uses a different copy control, inspect its manual-secret field and adapt the locator. Do not substitute a fixed secret from another account.
Pass the enrollment secret to MailSlurp, then request a code with at least ten seconds remaining before it expires:
const totpDevice = await mailslurp.mfaController.createTotpDeviceForBase32SecretKey({
createTotpDeviceBase32SecretKeyOptions: {
base32SecretKey: secret,
}
});
const otpCode = await mailslurp.mfaController.getTotpDeviceCode({
id: totpDevice.id,
minSecondsUntilExpire: 10
})
The enrollment secret can generate future codes, so treat it as a credential. Keep it and the OTP out of logs, screenshots, traces, and shared reports. The original sample contains screenshot calls around enrollment; remove those calls for CI and set Playwright's use.trace, use.screenshot, and use.video to "off" for this test.
Submit OTP code
Fill the current code, submit it, and accept the application's consent prompt in this sample:
await page.getByRole('textbox', { name: 'Enter your one-time code' }).fill(otpCode.code);
await page.getByRole('button', { name: 'Continue' }).click();
// accept connection
await page.getByRole('button', { name: 'Accept' }).click();
Now confirm that the browser returned to the app and can open the protected profile. Add this helper outside the test, with its imports at the top of the file:
import { expect, type Page } from "@playwright/test";
export async function assertAuth0Profile(
page: Page,
appUrl: string,
expectedEmail: string,
) {
const origin = new URL(appUrl).origin;
await page.waitForURL((url) => url.origin === origin, { timeout: 30_000 });
await page.goto(new URL("/#/profile", appUrl).toString());
await expect(page).toHaveURL(`${origin}/#/profile`);
await expect(page.locator(".profile-header p")).toHaveText(expectedEmail);
}
Merge type Page into the sample's existing Playwright import so expect is imported only once. Immediately after the Accept click, call:
await assertAuth0Profile(page, APP_URL, tempEmail);
The sample's profile component renders the authenticated user's email in .profile-header p, and its router guards /#/profile. Checking that exact email catches a session belonging to a different account. In another application, use its protected account view or authenticated account API to make the equivalent assertion.
Run the test from a second terminal with API_KEY and TEST_PASSWORD supplied:
npx playwright test playwright/mfa-totp.spec.ts --project=chromium --workers=1
Next steps
A returning-user test starts with an already-enrolled account. Keep the virtual device paired to that account, begin in a fresh browser context, request a new TOTP at the challenge, and repeat the identity assertion. Reusing a logged-in browser state can skip the very MFA interaction you intend to test; Playwright's authentication guide explains how saved state is reused.
If Auth0 rejects a code, check that the virtual device belongs to this enrollment and request the code immediately before submission. If the challenge never appears, check the tenant's MFA policy, remembered-device behavior, and browser state. Clean up the test identities, inboxes, and virtual devices when your suite no longer needs them.