Selenium
Test emails in Selenium
Selenium drives the browser; one HTTP request reads the email. The test signs up with a fresh {teamId}.signup-…@zyntramail.com address, asks the Zyntra API for the email and opens the link in it with the same WebDriver. The example is Python with pytest; the API call is the same from Java, C# or JavaScript.
# test_signup.py (pip install selenium requests pytest)
import os
import time
import uuid
import requests
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
API = "https://api.zyntra.app"
KEY = os.environ["ZYNTRA_API_KEY"]
TEAM = os.environ["ZYNTRA_TEAM_ID"]
BASE_URL = os.environ.get("BASE_URL", "https://staging.your-app.com")
def last_email(address, timeout=60):
# /emails/last waits up to 30 s on the server; ask again on 404.
deadline = time.monotonic() + timeout
while time.monotonic() < deadline:
r = requests.get(f"{API}/emails/last", params={"email": address},
headers={"X-API-Key": KEY}, timeout=45)
if r.ok:
return r.json()
if r.status_code != 404:
r.raise_for_status()
raise TimeoutError(f"No email for {address}")
def test_signup_confirms_email():
# A fresh address per run. Nothing to create first.
address = f"{TEAM}.signup-{uuid.uuid4().hex[:8]}@zyntramail.com"
driver = webdriver.Chrome()
try:
driver.get(f"{BASE_URL}/signup")
driver.find_element(By.NAME, "email").send_keys(address)
driver.find_element(By.NAME, "password").send_keys("a-long-test-password")
driver.find_element(By.CSS_SELECTOR, "button[type=submit]").click()
email = last_email(address)
assert "Confirm" in email["subject"]
confirm = next(l["url"] for l in email["links"] if "/verify" in l["url"])
driver.get(confirm)
WebDriverWait(driver, 10).until(EC.text_to_be_present_in_element(
(By.TAG_NAME, "body"), "Email confirmed"))
finally:
driver.quit()// Java 11+: the same read with java.net.http
var request = HttpRequest.newBuilder(URI.create(
"https://api.zyntra.app/emails/last?email="
+ URLEncoder.encode(address, StandardCharsets.UTF_8)))
.header("X-API-Key", System.getenv("ZYNTRA_API_KEY"))
.timeout(Duration.ofSeconds(45))
.build();
var response = HttpClient.newHttpClient()
.send(request, HttpResponse.BodyHandlers.ofString());
// response.body() is the email as JSON, with a "links" array.You need two values from the dashboard: an API key (API Keys page) and your team ID (bottom-left of the sidebar). Keep both in environment variables. Getting started shows where they are.
What happens to the email
- Your app sends it through its usual email provider, to an address on zyntramail.com.
- A real mail server receives it and stores it for your team, with its links pulled out of the HTML and text and its attachments kept.
- Your test reads it with one API request. Mail to an address you did not create in the dashboard is deleted after 1 day.
Why the email read is not a Selenium step
Selenium controls a browser, and opening a webmail inbox in that browser is slow, fragile and needs a real account. Reading the email over HTTP keeps the browser on your app: the test only switches to the link once it has it.
Grid and parallel runs
On Selenium Grid or any parallel runner, each test builds its own address, so no two sessions read the same email. The API call runs in the test process, not in the remote browser, so the Grid nodes need no access to Zyntra.
Limits to know about
/emails/lastwaits up to 30 seconds for the first email to an address. Give your HTTP client a longer timeout than that, as the example does withtimeout=45.- Every API call counts toward your plan's API requests: one read of an email is one request.
- Mail to these addresses is kept 1 day. Create a permanent inbox in the dashboard to keep it longer.
Questions
- Do I need a Selenium plugin or a mail library?
- No. Any HTTP client works:
requestsin Python,java.net.httpin Java. - How do I wait for a second email to the same address?
- Once an address has mail,
/emails/lastreturns the newest one at once. Check the subject and ask again every couple of seconds until it matches. The password-reset guide shows the pattern.
Add the email step to your Selenium suite
Free plan, no setup: send to any address and read it back.
Start free