AssertInbox

WordPress registration email test with Playwright

A WordPress site that allows registration sends every new user an email with a link to set a password. This guide tests that flow end to end with Playwright Test against a local WordPress: the test registers on wp-login.php?action=register with a unique address, waits for the email, opens the set-password link, chooses a password, logs in and checks the dashboard. A second test checks that a tampered link is rejected.

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 subject, links and, where one is found, otp and magic_link. Messages are deleted after 24 hours. WordPress puts its set-password link in the plain-text body, so the test picks it out of links by its action=rp query.

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 store it in your CI secrets.

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

Export both values for the tests: ASSERTINBOX_KEY is the api_key and ASSERTINBOX_NAMESPACE is 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 listed in the limits page.

A local WordPress with registration on

Save this as docker-compose.yml. It runs WordPress with a MariaDB database on port 8080. The database password is for this throwaway local setup only. The SMTP settings come from the environment and are used by the small plugin in the next section.

services:
  db:
    image: mariadb:11
    environment:
      MARIADB_DATABASE: wordpress
      MARIADB_USER: wp
      MARIADB_PASSWORD: wp-local-only
      MARIADB_RANDOM_ROOT_PASSWORD: "1"
    volumes:
      - db:/var/lib/mysql

  wordpress:
    image: wordpress:6-php8.3-apache
    depends_on:
      - db
    ports:
      - "8080:80"
    environment:
      WORDPRESS_DB_HOST: db
      WORDPRESS_DB_NAME: wordpress
      WORDPRESS_DB_USER: wp
      WORDPRESS_DB_PASSWORD: wp-local-only
      SMTP_HOST: ${SMTP_HOST}
      SMTP_PORT: ${SMTP_PORT:-587}
      SMTP_USER: ${SMTP_USER}
      SMTP_PASS: ${SMTP_PASS}
      SMTP_FROM: ${SMTP_FROM}
    volumes:
      - wp:/var/www/html
      - ./mail.php:/var/www/html/wp-content/mu-plugins/mail.php:ro

volumes:
  db:
  wp:

Start it, finish WordPress's install screen once in the browser, then switch on Anyone can register. You can tick it under Settings, then General, or use WP-CLI as shown. New users get the default role, which is Subscriber.

docker compose up -d
# open http://localhost:8080 once and finish the five-minute install, then:
docker compose run --rm --user 33:33 wordpress wp --path=/var/www/html option update users_can_register 1

Outgoing mail is your job

By default WordPress sends mail through PHP's mail() function. In a container that usually delivers nothing, because there is no mail server behind it. The test needs a working path to the address you register with, and the choice is yours: your staging provider's SMTP server, a relay you already use, or any SMTP plugin. Whatever you pick, the site then sends to assertinbox.com like to any other domain. As one way to wire it, save this as mail.php next to the compose file; the compose file mounts it as a must-use plugin, so there is nothing to activate. Set SMTP_HOST, SMTP_USER, SMTP_PASS and SMTP_FROM in your shell before docker compose up.

<?php
/**
 * Plugin Name: Test mail transport
 * Sends WordPress mail through an SMTP server that you choose, read from the environment.
 */
add_action('phpmailer_init', function ($phpmailer) {
    $host = getenv('SMTP_HOST');
    if (!$host) {
        return;
    }
    $phpmailer->isSMTP();
    $phpmailer->Host = $host;
    $phpmailer->Port = (int) (getenv('SMTP_PORT') ?: 587);
    $phpmailer->SMTPAuth = (bool) getenv('SMTP_USER');
    $phpmailer->Username = getenv('SMTP_USER') ?: '';
    $phpmailer->Password = getenv('SMTP_PASS') ?: '';
    $phpmailer->SMTPSecure = 'tls';
});

add_filter('wp_mail_from', function ($from) {
    return getenv('SMTP_FROM') ?: $from;
});

The helper and the tests

Save the helper as tests/inbox.js. It needs Node 18 or newer for the built-in fetch. The tag includes the worker index, a timestamp and a random part, so parallel workers and repeated runs never read the same inbox.

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

function newInbox(prefix, testInfo) {
  const random = Math.random().toString(36).slice(2, 8);
  const tag = (prefix + "-w" + testInfo.workerIndex + "-" + Date.now() + "-" + random).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 };

Save this as tests/register.spec.js. The locators are the ones WordPress's own login screens use; a theme or plugin that replaces those screens needs different ones. The test catches a missing weak-password checkbox on purpose, because it only appears when WordPress rates a password as weak.

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

const SITE = process.env.WP_URL || "http://localhost:8080";
const PASSWORD = "Correct-Horse-Battery-9!";

test("a visitor registers, sets a password from the email and logs in", async ({ page }, testInfo) => {
  const inbox = newInbox("wp", testInfo);
  const username = inbox.tag.replace(/[^a-z0-9]/g, "").slice(0, 40);
  try {
    await page.goto(SITE + "/wp-login.php?action=register");
    await page.fill("#user_login", username);
    await page.fill("#user_email", inbox.email);
    await page.click("#wp-submit");
    await expect(page.locator("#login-message, .message")).toContainText("Registration complete");

    const mail = await waitForEmail(inbox.tag);
    const setPassword = mail.links.find((link) => link.includes("action=rp"));
    expect(setPassword, "no set-password link in: " + mail.subject).toBeTruthy();

    await page.goto(setPassword);
    await page.fill("#pass1", PASSWORD);
    await page.check("#pw-weak").catch(() => {});
    await page.click("#wp-submit");
    await expect(page.locator(".message")).toContainText("password has been reset");

    await page.goto(SITE + "/wp-login.php");
    await page.fill("#user_login", username);
    await page.fill("#user_pass", PASSWORD);
    await page.click("#wp-submit");
    await expect(page).toHaveURL(/\/wp-admin\//);
    await expect(page.locator("#wpadminbar")).toBeVisible();
  } finally {
    await deleteEmails(inbox.tag);
  }
});

test("the link in the email works only for the new account", async ({ page }, testInfo) => {
  const inbox = newInbox("wp-bad", testInfo);
  try {
    await page.goto(SITE + "/wp-login.php?action=register");
    await page.fill("#user_login", inbox.tag.replace(/[^a-z0-9]/g, "").slice(0, 40));
    await page.fill("#user_email", inbox.email);
    await page.click("#wp-submit");

    const mail = await waitForEmail(inbox.tag);
    const setPassword = mail.links.find((link) => link.includes("action=rp"));
    await page.goto(setPassword.replace(/key=[^&]+/, "key=not-a-real-key"));
    await expect(page.locator("#login_error")).toContainText("invalid");
  } finally {
    await deleteEmails(inbox.tag);
  }
});

Run it with npx playwright test, with WP_URL set if your site is not on localhost:8080. The test creates a real user in the local database each time, so use a throwaway site, and clear users between runs if you need a clean list.

What the second test shows

The first test proves the happy path. The second takes the real email, changes the key in the link and expects WordPress to refuse it. That is a cheap way to confirm that the link in the email is the thing that grants access, and not just the user name in it. The exact wording of WordPress's messages depends on the version and the language, so adjust the expected text to your install.

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.