AssertInbox

Test localized emails in Playwright

An email that is correct in English can be broken in German: a missing translation key shows up as email.welcome.title, a placeholder arrives as {{name}}, or the subject falls back to English because the locale never reached the mail code. This guide tests localized emails in Playwright Test. One test is generated for each locale, the browser sends that locale with its sign-up request, and the test reads the email through AssertInbox and compares the subject and the body with the app's own translation files. It also checks the Content-Language header when your app sets it, and that no raw keys or placeholders leak into the text. It targets recent versions of @playwright/test and Node 18 or newer.

How it works

Mail sent to <namespace>.<tag>@assertinbox.com is stored for your key. GET /v1/wait long-polls and returns the newest matching message as JSON, including id, subject, text, html, headers and otp, or 408 if nothing arrived. Subjects and bodies come back decoded, so a German subject with an umlaut or a Japanese body is plain text in the response and needs no handling for MIME encoding. Messages are deleted after 24 hours.

Get an API key

Get one in a click at /start, or with the request below. One request creates a key and your private namespace. No email address or card is needed. The key is shown once, so keep it in your secret manager or CI secrets.

curl -s -X POST https://assertinbox.com/v1/keys
# {"api_key":"ai_...","namespace":"k3x9a1bz","address_template":"k3x9a1bz.<tag>@assertinbox.com"}

Export ASSERTINBOX_KEY (the api_key) and ASSERTINBOX_NAMESPACE (the namespace). If the call returns 403 signups_closed, the beta is not open for new keys. Use inboxes only for services you own or control; see the terms. Limits and error codes are on the limits page.

Set up the fixture

Download assertinbox.mjs and playwright-fixture.mjs from the client files page into your tests folder. The fixture adds an inbox object to every test. Its tag is unique for each test, worker and retry, so the tests for different locales never read each other's mail even when they run in parallel.

curl -O https://assertinbox.com/sdk/assertinbox.mjs
curl -O https://assertinbox.com/sdk/playwright-fixture.mjs
npm install --save-dev @playwright/test

What the test assumes about your app

The example assumes a sign-up page at /signup with an Email field and a Sign up button, and translation files in locales/, one JSON file per locale. Adapt the selectors and the keys to yours. The point is that the expected strings come from the same files the app uses, so a copy edit changes the app and the test together, and a missing entry fails the test instead of passing silently. A file might look like this:

{
  "email": {
    "subject": "Willkommen bei Beispiel",
    "greeting": "Hallo und willkommen",
    "button": "E-Mail-Adresse bestätigen"
  }
}

The test file

Save this as localized-email.spec.mjs and run npx playwright test localized-email.spec.mjs. The loop creates one describe block per locale. Inside it, test.use({ locale }) makes the browser send that locale in the Accept-Language header and report it in navigator.language, which is how most apps pick a language for a sign-up request. The app must be reachable at APP_URL, and it must send real mail to the address that the test types.

import { readFileSync } from "node:fs";
import { test, expect } from "./playwright-fixture.mjs";

const APP = process.env.APP_URL || "http://localhost:3000";
const LOCALES = ["en", "de", "fr"];

const messages = (locale) =>
  JSON.parse(readFileSync(new URL("./locales/" + locale + ".json", import.meta.url), "utf8"));

// Things that must never appear in a finished email.
const LEAKS = [/\{\{\s*\w+\s*\}\}/, /%\{\w+\}/, /\{\w+\}/, /\bundefined\b/, /\[object Object\]/, /\b[a-z]+\.[a-z]+\.[a-z]+\b/];

for (const locale of LOCALES) {
  test.describe("email in " + locale, () => {
    test.use({ locale });

    test("subject and body follow the locale", async ({ page, inbox }) => {
      const copy = messages(locale).email;

      await page.goto(APP + "/signup");
      await page.getByLabel("Email").fill(inbox.address("l10n-" + locale));
      await page.getByRole("button", { name: "Sign up" }).click();

      const mail = await inbox.wait();

      // Use the plain-text part, or the HTML with its tags removed when the email has none.
      const body = mail.text || mail.html.replace(/<[^>]+>/g, " ");

      expect(mail.subject).toBe(copy.subject);
      expect(body).toContain(copy.greeting);
      expect(body).toContain(copy.button);

      for (const leak of LEAKS) {
        expect(mail.subject).not.toMatch(leak);
        expect(body).not.toMatch(leak);
      }
    });

    test("the language header matches when the app sets one", async ({ page, inbox }) => {
      await page.goto(APP + "/signup");
      await page.getByLabel("Email").fill(inbox.address("hdr-" + locale));
      await page.getByRole("button", { name: "Sign up" }).click();

      const mail = await inbox.wait();
      const header = mail.headers["content-language"];
      test.skip(!header, "the app sends no Content-Language header");
      expect(header.toLowerCase().split("-")[0]).toBe(locale);
    });
  });
}

Some apps send only an HTML part and some only text, which is why the test reads text first and falls back to the HTML with its tags removed. Removing the tags also keeps link addresses inside href attributes out of the leak check. The leak patterns are deliberately broad. The first three catch placeholders in the styles of Handlebars, Rails and ICU. The last one catches a dotted key such as email.welcome.title that was never replaced by text. A real URL in the body does not match it, because the pattern only accepts three lower-case words joined by dots with no scheme or slash, but a sentence that happens to mention www.example.com would; narrow the pattern to your key style if that happens.

Test the fallback

Add one more case for a locale the app does not support, such as pt-BR when you ship only three languages. The expected email is in the default language, and the test is the only thing that will notice if a refactor turns the fallback into an error or into a key. Use the same flow with test.use({ locale: "pt-BR" }) and compare with the default locale's strings. If your app stores a user's language, add a test that signs up in German and then triggers a second email, such as a password reset, to prove the stored language is used outside the sign-up request.

Run it in CI

Store ASSERTINBOX_KEY and ASSERTINBOX_NAMESPACE as secrets in your CI provider and pass them as environment variables to the step that runs npx playwright test. Never print the key. Each locale costs two sign-ups and one wait, so even ten languages stay far inside the daily limits. If your app limits sign-ups per address or network, run the locale tests against a staging environment with the limit raised.

Troubleshooting

Related guides: test transactional email content, email verification in Playwright.

Next steps

For unique tags, the trigger-then-wait order, retries and cleanup in one place, read Reliable email tests. More stacks are on the guides page.