Email tests in GitHub Actions
Email-verification tests that pass on a laptop can fail in CI for reasons that have nothing to do with the app: the API key is missing, two jobs read the same inbox, or a slow runner hits a timeout. This guide sets up Playwright tests that read verification emails through AssertInbox in a GitHub Actions workflow, with the key stored as a repository secret, one set of inboxes per shard, sensible timeouts and cleanup after each test.
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
In your repository, open Settings, then Secrets and variables, then Actions. Create a repository secret named ASSERTINBOX_KEY with the api_key value. The namespace is not secret, since it is only the first part of an email address, so store it as a repository variable named ASSERTINBOX_NAMESPACE. Add APP_URL as a variable too if your tests run against a preview or staging URL.
GitHub does not pass repository secrets to workflows triggered by pull requests from forks, so email tests on such runs will fail with a missing key. Decide whether those runs should skip the email tests; do not work around it by printing the key or by using an event type that exposes secrets to untrusted code.
The workflow
Save this as .github/workflows/e2e.yml. The key goes into the job's environment from the secret, so only that job can read it. The workflow limits itself to read access to the repository contents and stops the job after 15 minutes.
name: e2e
on:
push:
branches: [main]
pull_request:
permissions:
contents: read
jobs:
e2e:
runs-on: ubuntu-latest
timeout-minutes: 15
env:
ASSERTINBOX_KEY: ${{ secrets.ASSERTINBOX_KEY }}
ASSERTINBOX_NAMESPACE: ${{ vars.ASSERTINBOX_NAMESPACE }}
APP_URL: ${{ vars.APP_URL }}
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
- run: npm ci
- run: npx playwright install --with-deps chromium
- run: npx playwright test
Keeping the key out of the logs
- GitHub hides the exact secret value in logs, but only if it is registered as a secret. Do not build the key from pieces or transform it before use.
- Never run
env,printenvorset -xin a step that has the key. Do not log request headers. - Send the key only in the
Authorizationheader, never in a URL, which can end up in logs and error messages. - Throw errors that contain the HTTP status and the tag, as the helper below does, and not the request.
- If the key leaks, call
POST /v1/keys/me/rotatewith it to get a new one, then update the secret.
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, which the workflow sets per shard, with a counter and a random suffix. Two shards, two re-runs of the same workflow, or two tests in one file can therefore never read each other's mail.
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);
}
throw new Error("No 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 };
The test below goes in tests/signup.spec.js. Replace the selectors and the success text with those of your app.
const { test, expect } = require("@playwright/test");
const { newInbox, waitForEmail, deleteEmails } = require("./inbox");
test.describe("sign-up email verification", () => {
let inbox;
test.beforeEach(async ({ page }) => {
inbox = newInbox("signup");
await page.goto("/signup");
await page.fill("input[name=email]", inbox.email);
await page.click("button[type=submit]");
});
test.afterEach(async () => {
await deleteEmails(inbox.tag);
});
test("verifies with the one-time code", async ({ page }) => {
const mail = await waitForEmail(inbox.tag);
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.locator("body")).toContainText("Email verified");
});
});
Add a small configuration in playwright.config.js. The test timeout of 120 seconds is longer than one email wait, and a single retry on CI covers a flaky page load without hiding a missing email.
const { defineConfig } = require("@playwright/test");
module.exports = defineConfig({
testDir: "./tests",
timeout: 120000,
retries: process.env.CI ? 1 : 0,
workers: 2,
reporter: process.env.CI ? [["github"], ["html", { open: "never" }]] : "list",
use: { baseURL: process.env.APP_URL || "http://localhost:3000" },
});
Sharding
Playwright can split a suite across several jobs with --shard=1/4. Each job runs on its own machine, but all of them share one API key, and unique tags are what keep them apart. The matrix below sets RUN_TAG from the run id, the attempt number and the shard number. Setting fail-fast: false lets the other shards finish when one fails, so you see every failing test in one run.
name: e2e-sharded
on:
push:
branches: [main]
permissions:
contents: read
jobs:
e2e:
runs-on: ubuntu-latest
timeout-minutes: 20
strategy:
fail-fast: false
matrix:
shard: [1, 2, 3, 4]
env:
ASSERTINBOX_KEY: ${{ secrets.ASSERTINBOX_KEY }}
ASSERTINBOX_NAMESPACE: ${{ vars.ASSERTINBOX_NAMESPACE }}
APP_URL: ${{ vars.APP_URL }}
RUN_TAG: gh-${{ github.run_id }}-${{ github.run_attempt }}-${{ matrix.shard }}
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
- run: npm ci
- run: npx playwright install --with-deps chromium
- run: npx playwright test --shard=${{ matrix.shard }}/4
All shards count against the same daily request allowance for the key, 2,000 calls. A passing email test uses one call to wait and one to delete. If the suite grows past that, the API returns 429 with a Retry-After header; see the limits page before adding more shards.
Timeouts
There are three clocks, and they must be nested. One email wait lasts up to 25 seconds. The helper tries up to three times, so a missing email is reported after about 75 seconds. The Playwright test timeout must be longer than that, and the job's timeout-minutes must be longer than the whole suite on the slowest shard. When the clocks are in the wrong order, the symptom is a generic timeout that hides the real cause.
Cleanup
The afterEach hook deletes the messages for the test's tag, so a failed run leaves little behind. Messages are deleted after 24 hours anyway. If a run is cancelled midway, the hooks do not run, and the 24-hour deletion covers that case.
Troubleshooting
- "Set ASSERTINBOX_KEY and ASSERTINBOX_NAMESPACE". The job did not receive them. Check the secret name, and check whether the run came from a fork.
- HTTP 401. The secret holds a wrong or rotated key.
- HTTP 429. The key used its calls for the day.
Retry-Aftergives the seconds until 00:00 UTC. - A shard fails with no email. Open the failing test's log, find the address in the tag, and check your app's logs for a send to it.
Related guides: Playwright email verification, testing password reset emails, sign-up email tests in Node.