Using MailFlat with Selenium
Fill a signup form with a real address, then type the code that really arrived — in Java or Python, next to the WebDriver calls you already have.
How it fits together
Selenium drives the browser and nothing else, so the mailbox has to come from somewhere. MailFlat sits beside the driver: open an inbox, send its address through the form, wait for the code, type it in.
Java, with JUnit 5
A fresh inbox per test, torn down in the AfterEach hook.
import net.mailflat.MailFlat;
import net.mailflat.Inbox;
import org.junit.jupiter.api.*;
import org.openqa.selenium.By;
class SignupTest {
static MailFlat mf = new MailFlat(System.getenv("MAILFLAT_API_KEY"));
Inbox inbox;
@BeforeEach
void openInbox() {
inbox = mf.create("selenium-" + System.nanoTime());
}
@AfterEach
void closeInbox() {
inbox.delete();
}
@Test
void signsUpWithARealCode() {
driver.get("https://staging.example.com/signup");
driver.findElement(By.id("email")).sendKeys(inbox.address());
driver.findElement(By.id("submit")).click();
driver.findElement(By.id("code")).sendKeys(inbox.waitForOtp(60));
Assertions.assertTrue(driver.getCurrentUrl().endsWith("/dashboard"));
}
}
Python, with pytest
Same shape, in a fixture.
import os, time, pytest
from mailflat import MailFlat
from selenium.webdriver.common.by import By
mf = MailFlat(api_key=os.environ["MAILFLAT_API_KEY"])
@pytest.fixture
def inbox():
box = mf.create(prefix=f"selenium-{int(time.time())}", retention_hours=2)
yield box
box.delete()
def test_signs_up_with_a_real_code(driver, inbox):
driver.get("https://staging.example.com/signup")
driver.find_element(By.ID, "email").send_keys(inbox.address)
driver.find_element(By.ID, "submit").click()
driver.find_element(By.ID, "code").send_keys(inbox.wait_for_otp(timeout=60))
assert driver.current_url.endswith("/dashboard")
Worth knowing
waitForOtp blocks the calling thread, so there is no need for an explicit WebDriverWait around it.
Grid and parallel runs are safe: the isolation comes from one inbox per test, not from the driver.
See also
Java
Built on java.net.http, no HTTP dependency of its own. Made for Selenium and JUnit suites that need a real address per test.
PythonThe official Python client wraps the REST API in two calls: open an inbox, wait for the code. Works in pytest, unittest, Robot Framework or a plain script.
One-time codesMailFlat pulls the verification code out of the message for you and hands it over as a field, so your test never runs a regex against an email body.