AssertInbox

Run email tests in Azure Pipelines

Email tests are easy to run on a laptop and easy to break in CI: the key never reaches the step that needs it, two builds read each other's messages, or a parallel job waits for a mail that another job consumed. This guide sets up Playwright Test in Azure Pipelines with the AssertInbox API key stored as a secret variable and mapped into the step explicitly, a tag that is unique for every build and every parallel job, and a job that fails with a readable message. It uses a complete azure-pipelines.yml, a helper and a spec.

How it works

Mail sent to <namespace>.<tag>@assertinbox.com is stored for your key. GET /v1/wait long-polls for up to 25 seconds and returns the newest matching message as JSON, including id, subject, otp and magic_link. Messages are deleted after 24 hours. The tag is the isolation boundary: if every job builds its tags from the build id and their own position in the job group, no two jobs can read the same mail.

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.

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

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.

Store the key as a secret variable

In your pipeline, choose Edit, then Variables, and add ASSERTINBOX_KEY with the api_key value. Turn on Keep this value secret. Azure Pipelines masks secret values in logs. The namespace is not secret, since it is only the first part of an address, so add it as a normal variable named ASSERTINBOX_NAMESPACE. Add APP_URL too if your tests run against a staging URL. To share the values between pipelines, use a variable group instead and reference it in the YAML.

Secrets are not exposed to scripts automatically. A secret variable is not placed in the environment of a script step. You have to map it into the step's env yourself, as the pipeline below does. If the test cannot see the key, this is the first thing to check.

The pipeline

Save this as azure-pipelines.yml. strategy.parallel: 4 runs four copies of the job, and each copy gets System.JobPositionInPhase (counting from 1) and System.TotalJobsInPhase. Playwright's --shard option counts from 1 too, so the values go straight in. RUN_TAG combines Build.BuildId with the job position, so every build and every parallel job get their own tags. The step installs the browser with its system dependencies; keep Playwright's version pinned in your package.json.

trigger:
  - main

pr:
  - main

pool:
  vmImage: ubuntu-latest

jobs:
  - job: e2e
    timeoutInMinutes: 20
    strategy:
      parallel: 4
    variables:
      RUN_TAG: "azp-$(Build.BuildId)-$(System.JobPositionInPhase)"
    steps:
      - task: NodeTool@0
        inputs:
          versionSpec: "20.x"
      - script: npm ci
        displayName: Install dependencies
      - script: npx playwright install --with-deps chromium
        displayName: Install browser
      - script: npx playwright test --shard=$(System.JobPositionInPhase)/$(System.TotalJobsInPhase)
        displayName: Run email tests
        env:
          ASSERTINBOX_KEY: $(ASSERTINBOX_KEY)
          ASSERTINBOX_NAMESPACE: $(ASSERTINBOX_NAMESPACE)
          APP_URL: $(APP_URL)
      - task: PublishPipelineArtifact@1
        condition: failed()
        inputs:
          targetPath: playwright-report
          artifact: playwright-report-$(System.JobPositionInPhase)

Only the test step has the secret in its environment, so the install steps and any third-party task never see it. The normal variable RUN_TAG is available to every step as an environment variable. Pull requests from forks do not receive secret variables unless you change the pipeline settings; decide whether those runs should skip the email tests. The published report and traces show the page state when a test fails. They do not contain the key, as long as your tests never print it.

The helper and the test

Save the helper as tests/inbox.js. It needs Node 18 or newer for the built-in fetch. The tag combines RUN_TAG with a counter and a random suffix.

const API = "https://assertinbox.com";
const KEY = process.env.ASSERTINBOX_KEY;
const NAMESPACE = process.env.ASSERTINBOX_NAMESPACE;

if (!KEY || !NAMESPACE) {
  throw new Error("Set ASSERTINBOX_KEY and ASSERTINBOX_NAMESPACE");
}

let counter = 0;

function newInbox(label) {
  const base = process.env.RUN_TAG || "local-" + Date.now();
  const tag = (base + "-" + label + "-" + ++counter + "-" + Math.random().toString(36).slice(2, 6)).toLowerCase();
  return { tag, email: NAMESPACE + "." + tag + "@assertinbox.com" };
}

async function waitForEmail(tag, filters = {}) {
  const params = new URLSearchParams({ tag, timeout: "25", ...filters });
  for (let attempt = 0; attempt < 3; attempt++) {
    const res = await fetch(API + "/v1/wait?" + params, {
      headers: { Authorization: "Bearer " + KEY },
    });
    if (res.status === 200) return res.json();
    if (res.status !== 408) throw new Error("AssertInbox returned HTTP " + res.status + " for tag " + tag);
  }
  throw new Error("No matching email arrived for tag " + tag);
}

async function deleteEmails(tag) {
  await fetch(API + "/v1/messages?tag=" + encodeURIComponent(tag), {
    method: "DELETE",
    headers: { Authorization: "Bearer " + KEY },
  });
}

module.exports = { newInbox, waitForEmail, deleteEmails };

Save this as tests/signup.spec.js. Replace the page, labels and subject with those of your app.

const { test, expect } = require("@playwright/test");
const { newInbox, waitForEmail, deleteEmails } = require("./inbox");

const APP = process.env.APP_URL || "http://localhost:3000";

test("sign-up with an emailed code", async ({ page }) => {
  const inbox = newInbox("signup");
  try {
    await page.goto(APP + "/signup");
    await page.fill("input[name=email]", inbox.email);
    await page.click("button[type=submit]");

    const mail = await waitForEmail(inbox.tag, { subject: "verify" });
    expect(mail.otp).toMatch(/^[0-9]{4,8}$/);

    await page.fill("input[name=code]", mail.otp);
    await page.click("button[type=submit]");
    await expect(page.getByText("Email verified")).toBeVisible();
  } finally {
    await deleteEmails(inbox.tag);
  }
});

Keeping the key out of the logs

Parallel jobs and the daily allowance

Each key has 2,000 API requests per UTC day, and a wait counts as one request however long it polls. A few parallel jobs that each run a few dozen email tests stay far below that, but a pipeline that runs on every push of a busy repository can add up. If you want to separate pipelines from local runs, create a second key for CI. If a job fails with HTTP 429, the Retry-After header gives the seconds until 00:00 UTC.

Troubleshooting

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.