Playwright for Java: email OTP test with JUnit 5
Playwright for Java drives a real browser from JUnit 5, and a sign-up test needs a real inbox for the email step. This guide uses the long-standing Playwright API (Playwright.create(), page.navigate, getByLabel, getByRole) with the single-file Java client. Each test gets a unique address, waits for the email, types the one-time code and covers the resend button.
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, with otp, magic_link and links already extracted. Messages are deleted after 24 hours. A unique tag per test is a private inbox, so tests running in parallel never read each other's mail.
Get an API key and the client file
Get a key in a click at /start. One request creates a key and your private namespace, with no email address or card. The key is shown once, so keep it in your CI secret store and export ASSERTINBOX_KEY (the api_key) and ASSERTINBOX_NAMESPACE (the namespace). Use inboxes only for services you own or control; see the terms. Limits and error codes are on the limits page.
curl -o src/test/java/AssertInbox.java https://assertinbox.com/sdk/AssertInbox.java
The client needs Java 11 or later and nothing else. Add com.microsoft.playwright:playwright and org.junit.jupiter:junit-jupiter as test dependencies. Playwright downloads its browsers the first time it runs, which can take a while on a fresh CI machine; cache the download directory between runs.
The test
Save this as src/test/java/OtpSignUpTest.java. The URL, labels and success text are placeholders for your own app. The browser, context and page are created per test, so tests do not share cookies or storage.
import static com.microsoft.playwright.assertions.PlaywrightAssertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import com.microsoft.playwright.Browser;
import com.microsoft.playwright.BrowserContext;
import com.microsoft.playwright.Page;
import com.microsoft.playwright.Playwright;
import com.microsoft.playwright.options.AriaRole;
import java.util.Map;
import java.util.regex.Pattern;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
class OtpSignUpTest {
static final String APP = System.getenv().getOrDefault("APP_URL", "http://localhost:3000");
final AssertInbox inbox = new AssertInbox(System.getenv("ASSERTINBOX_KEY"), System.getenv("ASSERTINBOX_NAMESPACE"));
String tag;
Playwright playwright;
Browser browser;
BrowserContext context;
Page page;
@BeforeEach
void setUp() {
tag = inbox.uniqueTag("pw");
playwright = Playwright.create();
browser = playwright.chromium().launch();
context = browser.newContext();
page = context.newPage();
}
@AfterEach
void tearDown() {
context.close();
browser.close();
playwright.close();
inbox.deleteMessages(tag);
}
void submitSignUp() {
page.navigate(APP + "/signup");
page.getByLabel("Email").fill(inbox.address(tag));
page.getByRole(AriaRole.BUTTON, new Page.GetByRoleOptions().setName("Sign up")).click();
}
@Test
void signUpWithEmailedCode() {
submitSignUp();
String code = inbox.waitForOtp(tag, Map.of("subject", "verify"));
page.getByLabel("Code").fill(code);
page.getByRole(AriaRole.BUTTON, new Page.GetByRoleOptions().setName("Verify")).click();
assertThat(page.getByText(Pattern.compile("welcome", Pattern.CASE_INSENSITIVE))).isVisible();
}
@Test
void signUpWithMagicLink() {
submitSignUp();
String link = inbox.waitForLink(tag, Map.of("subject", "verify"));
assertNotNull(link);
page.navigate(link);
assertThat(page.getByText("Email verified")).isVisible();
}
}
Run it with mvn test or gradle test. Playwright's assertThat assertions retry until the element appears or their timeout passes, so the test needs no sleeps after a page change.
Resend with after
A "resend code" button sends a new message to the same address, and /v1/wait returns the newest message for the tag, which could be the old one. Pass the id of the message you already read as after and the call returns only a newer message. Add this method to the same class:
@Test
void resendSendsANewCode() {
submitSignUp();
AssertInbox.Message first = inbox.waitFor(tag, Map.of("subject", "verify"));
page.getByRole(AriaRole.BUTTON, new Page.GetByRoleOptions().setName("Resend code")).click();
AssertInbox.Message second = inbox.waitFor(tag, Map.of("subject", "verify", "after", first.id));
page.getByLabel("Code").fill(second.otp);
page.getByRole(AriaRole.BUTTON, new Page.GetByRoleOptions().setName("Verify")).click();
assertThat(page.getByText("Email verified")).isVisible();
}
Two kinds of waiting
The email wait is a server-side long poll: one HTTP call that returns as soon as the message arrives, and waitFor repeats it up to two more times after a 408 timeout by default. The page waits are Playwright's auto-waiting actions and retrying assertions. Keep them separate, and avoid Thread.sleep and page.waitForTimeout for this flow.
Parallel runs
Every test builds its own tag, its own Playwright instance, browser and context, so JUnit 5 parallel execution (junit.jupiter.execution.parallel.enabled=true) is safe. Playwright objects are not thread-safe, which is why the example creates them in setUp instead of sharing them in a static field. If your app rate-limits sign-ups per source, lower the parallelism rather than reusing addresses.
Troubleshooting
- AssertInboxException with code timeout. The app never sent the email, or sent it to another address. Print
inbox.address(tag)and compare it with the app's logs. - Code unauthorized, status 401. The key is wrong or missing in the environment that runs the tests.
- HTTP 429. The key used its 2,000 requests for the day.
GET /v1/meshows usage. - IllegalStateException about a missing code. The email has no recognisable code. Read the message text and parse it yourself; the full body is always returned.
- Browsers are missing in CI. Run the Playwright install step once, or cache the browser directory, before the tests start.
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.