Playwright
A Playwright test that signs up with a fresh address, waits for the code in the mail and types it in.
Talking to Mayfly
Two functions, in a file of their own. mayflyNow reads Mayfly's clock from the Date header of one of its answers. waitForMail holds until the mail has arrived. Run the tests with your API key in MAYFLY_API_KEY.
const MAYFLY = "https://mayfly.aravaltech.com";
const headers = { Authorization: `Bearer ${process.env.MAYFLY_API_KEY}` };
// Mayfly's clock, from the Date header of one of its answers.
// Take it before your app sends the mail.
export async function mayflyNow(): Promise<string> {
const response = await fetch(`${MAYFLY}/api/v1/mailboxes?limit=1`, { headers });
if (!response.ok) throw new Error(`Mayfly answered ${response.status}`);
return new Date(response.headers.get("date")!).toISOString();
}
// The newest mail to `to` since `since`, once it has arrived and been read.
export async function waitForMail(
to: string,
since: string,
more: Record<string, string> = {},
) {
const query = new URLSearchParams({ to, since, timeout: "30", ...more });
const response = await fetch(`${MAYFLY}/api/v1/wait?${query}`, { headers });
if (!response.ok) {
throw new Error(`Mayfly answered ${response.status}: ${await response.text()}`);
}
return response.json();
}The test
Take since before the app sends anything. Then no older mail to the address can be picked up, whatever the test machine's clock says.
import { expect, test } from "@playwright/test";
import { mayflyNow, waitForMail } from "./mayfly";
const DOMAIN = "<your-name>.qa.aravaltech.info";
test("sign-up sends a code that works", async ({ page }) => {
test.setTimeout(70_000); // the wait alone can take 30 seconds
const email = `signup-${Date.now()}@${DOMAIN}`;
const since = await mayflyNow();
await page.goto("/sign-up");
await page.getByLabel("Email").fill(email);
await page.getByRole("button", { name: "Send code" }).click();
const mail = await waitForMail(email, since);
expect(mail.otp, "Mayfly found no code in the mail").not.toBeNull();
await page.getByLabel("Code").fill(mail.otp.value);
await page.getByRole("button", { name: "Verify" }).click();
await expect(page.getByRole("heading", { name: "Welcome" })).toBeVisible();
});Put your own app's page and labels in, and your private test domain in place of <your-name>: it's on Overview. mail.otp is null when Mayfly found no code in the mail, and its confidence is low when it found more than one candidate, or no keyword near it. The whole mail is in the API reference.
Links instead of codes
mail.links lists every link in the mail. Mail is written by strangers, even in a test, so only follow a link to your own app.
const mail = await waitForMail(email, since);
const ownOrigin = new URL(page.url()).origin;
const link = mail.links.find(
(href: string) => URL.canParse(href) && new URL(href).origin === ownOrigin,
);
expect(link, "no link to this app in the mail").toBeTruthy();
await page.goto(link!);A second mail
Take a fresh since before each mail you ask for. When one address gets several kinds of mail, pick one out by part of its subject, or with from.
// A fresh `since` before each mail you ask for.
const sinceResend = await mayflyNow();
await page.getByRole("button", { name: "Send a new code" }).click();
const second = await waitForMail(email, sinceResend);
// Two kinds of mail to one address: pick one out by its subject.
const welcome = await waitForMail(email, since, { subject: "Welcome" });