AssertInbox

Test an order confirmation email in Playwright

An order confirmation is the email a customer keeps. It shows the order number, what was bought and what was charged, and a wrong total or a missing invoice generates support tickets. This guide tests it in Playwright Test against your own staging shop: place an order with the payment provider's test mode, wait for the email through AssertInbox, and assert that the order number, the line items and the total in the email match what the order page showed. When the shop attaches an invoice PDF, the test also checks the attachment by name and type. It targets recent versions of @playwright/test and Node 18 or newer. The test uses a staging shop that you control and a test payment mode that you configure; it never places a real order or charges a real card.

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 and attachments, or 408 if nothing arrived. Each entry in attachments has filename, content_type and size. The file's bytes are not stored, so the test can prove that an invoice of a plausible size was attached but cannot read its pages. 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, it waits with the 408 retry already built in, and it deletes the test's messages when the test ends.

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

Prepare the shop

Run the test against a staging copy of the shop whose payment provider is in test mode, so no money moves. Most providers document test card numbers or a test checkout for exactly this; use theirs and keep the value in an environment variable, not in the repository. The example below calls placeOrder, which holds every shop-specific step: choosing products, entering the address and paying in test mode. Its selectors are placeholders for your own. Everything after it is generic.

Read money from the page and the email

Compare amounts as integers in the smallest currency unit, never as formatted strings. A page may show 1,234.50 and an email 1234.50 USD; both are the same amount. The helper below finds the first money-like value in a piece of text and returns cents. It assumes a dot as the decimal separator and an optional comma for thousands, so change it if your shop uses other formats.

The test file

Save this as order-confirmation.spec.mjs and run npx playwright test order-confirmation.spec.mjs. The test reads the order number, the lines and the total from the order page after checkout, then waits for the email and asserts each of them against it. The number is taken from the page and not typed into the test, so the test keeps working as numbering changes.

import { test, expect } from "./playwright-fixture.mjs";

const SHOP = process.env.SHOP_URL || "http://localhost:3000";

const cents = (text) => {
  const m = String(text).match(/([0-9]{1,3}(?:,[0-9]{3})*|[0-9]+)\.([0-9]{2})/);
  if (!m) throw new Error("no amount in: " + text);
  return Number(m[1].replace(/,/g, "")) * 100 + Number(m[2]);
};

// Everything specific to your shop lives here: replace the steps and selectors.
async function placeOrder(page, email) {
  await page.goto(SHOP + "/shop");
  for (const name of ["Blue mug", "Tote bag"]) {
    await page.getByRole("listitem").filter({ hasText: name }).getByRole("button", { name: "Add to cart" }).click();
  }
  await page.getByRole("link", { name: "Checkout" }).click();
  await page.getByLabel("Email").fill(email);
  await page.getByLabel("Card number").fill(process.env.TEST_CARD_NUMBER || "");
  await page.getByRole("button", { name: "Place order" }).click();
  await expect(page.getByRole("heading", { name: /Order #/ })).toBeVisible();
}

async function readOrder(page) {
  const heading = await page.getByRole("heading", { name: /Order #/ }).innerText();
  const number = heading.match(/#\s*([A-Za-z0-9-]+)/)[1];
  const rows = await page.getByRole("row").filter({ hasText: /[0-9]\.[0-9]{2}/ }).allInnerTexts();
  const lines = rows.map((row) => {
    const cells = row.split(/\t+/).map((c) => c.trim());
    return { name: cells[0], quantity: Number(cells[1]), total: cents(cells[cells.length - 1]) };
  });
  const total = cents(await page.getByTestId("order-total").innerText());
  return { number, lines, total };
}

test("the confirmation email matches the order page", async ({ page, inbox }) => {
  await placeOrder(page, inbox.address("order"));
  const order = await readOrder(page);
  expect(order.lines.length).toBeGreaterThan(0);

  const mail = await inbox.wait();
  const body = mail.text || mail.html.replace(/<[^>]+>/g, " ");

  expect(mail.subject).toContain(order.number);
  expect(body).toContain(order.number);

  for (const line of order.lines) {
    expect(body).toContain(line.name);
  }

  const totals = [...body.matchAll(/(?:total|gesamt)[^0-9\n]*([0-9][0-9,]*\.[0-9]{2})/gi)].map((m) => cents(m[1]));
  expect(totals.at(-1)).toBe(order.total);
  expect(order.lines.reduce((sum, line) => sum + line.total, 0)).toBeLessThanOrEqual(order.total);
});

test("the invoice PDF is attached when the shop sends one", async ({ page, inbox }) => {
  await placeOrder(page, inbox.address("invoice"));
  const order = await readOrder(page);

  const mail = await inbox.wait();
  const pdfs = mail.attachments.filter((a) => a.content_type === "application/pdf");
  test.skip(pdfs.length === 0 && !process.env.EXPECT_INVOICE, "this shop sends no invoice PDF");

  expect(pdfs).toHaveLength(1);
  expect(pdfs[0].filename).toContain(order.number);
  expect(pdfs[0].filename).toMatch(/\.pdf$/i);
  expect(pdfs[0].size).toBeGreaterThan(1000);
});

Four choices keep this test honest. The expected values come from the page the customer saw, so a bug that shows one total on the page and another in the email fails the test. The total is matched against the last total amount in the email, because emails often list a subtotal, shipping and tax first. The sum of the line totals is only required to be at most the order total, since shipping and tax sit on top, and your own shop may deserve an exact equation; add it when you know the rules. And the PDF test skips unless the shop is known to send one: set EXPECT_INVOICE=1 once it does, so that a missing invoice then fails instead of skipping.

What to assert, and what not to

Assert the facts a customer relies on: the number, the items, the quantities and the amounts. Do not assert the greeting, the marketing footer or the exact layout; copy changes would break the suite without finding a bug. Do not assert the name on the card or an address unless your shop prints them. If your shop sends separate emails for payment and for shipping, wait for each with inbox.wait({ after: first.id }) and give each its own test.

Run it in CI

Store ASSERTINBOX_KEY, ASSERTINBOX_NAMESPACE and TEST_CARD_NUMBER as secrets in your CI provider and pass them as environment variables to the step that runs npx playwright test. Never print them. Point SHOP_URL at the staging shop and confirm that its payment provider is in test mode before the suite runs; a check that the page shows your provider's test-mode notice is a cheap safeguard. Each test creates one order, so clean orders up in staging on a schedule if the shop keeps them.

Troubleshooting

Related guides: test transactional email content, test localized emails.

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.