Examples
Mocha + Selenium
Learn how to test email workflows using Trivumo and Selenium WebDriver with Mocha.
Test email-driven authentication flows in end-to-end Selenium tests using Trivumo.
Install
npm install selenium-webdriver mocha trivumoCreate a Client
const { Builder, By, until } = require("selenium-webdriver");
const { TrivumoClient } = require("trivumo");
const trivumo = new TrivumoClient({
apiKey: process.env.TRIVUMO_API_KEY,
});Verify Email Signup Flow
const assert = require("assert");
const { Builder, By, until } = require("selenium-webdriver");
const { TrivumoClient } = require("trivumo");
describe("Email Verification", function () {
this.timeout(120000);
it("verifies a newly created account", async function () {
const driver = await new Builder().forBrowser("chrome").build();
const trivumo = new TrivumoClient({
apiKey: process.env.TRIVUMO_API_KEY,
});
const email = await trivumo.generate_email();
try {
await driver.get("https://example.com/signup");
await driver.findElement(
By.NAME("email"),
).sendKeys(email.address);
await driver.findElement(
By.NAME("password"),
).sendKeys("Password123!");
await driver.findElement(
By.CSS_SELECTOR("button[type='submit']"),
).click();
const message = await trivumo.wait_for_message({
to: email.address,
subject: "Verify your account",
});
const verification_link =
message.html.links[0].href;
await driver.get(verification_link);
const success_message = await driver.wait(
until.elementLocated(
By.xpath("//*[contains(text(),'Email verified')]"),
),
10000,
);
assert.ok(await success_message.isDisplayed());
} finally {
await driver.quit();
}
});
});Password Reset Flow
const assert = require("assert");
const { Builder, By, until } = require("selenium-webdriver");
const { TrivumoClient } = require("trivumo");
describe("Password Reset", function () {
this.timeout(120000);
it("resets a password using the email link", async function () {
const driver = await new Builder().forBrowser("chrome").build();
const trivumo = new TrivumoClient({
apiKey: process.env.TRIVUMO_API_KEY,
});
const email = await trivumo.generate_email();
try {
await driver.get("https://example.com/forgot-password");
await driver.findElement(
By.NAME("email"),
).sendKeys(email.address);
const requested_at = new Date().toISOString();
await driver.findElement(
By.CSS_SELECTOR("button[type='submit']"),
).click();
const message =
await trivumo.wait_for_message_after(
requested_at,
{
to: email.address,
subject: "Reset your password",
},
);
const reset_link =
message.html.links[0].href;
await driver.get(reset_link);
await driver.findElement(
By.NAME("password"),
).sendKeys("NewPassword123!");
await driver.findElement(
By.NAME("confirmPassword"),
).sendKeys("NewPassword123!");
await driver.findElement(
By.CSS_SELECTOR("button[type='submit']"),
).click();
const success_message = await driver.wait(
until.elementLocated(
By.xpath(
"//*[contains(text(),'Password updated')]",
),
),
10000,
);
assert.ok(await success_message.isDisplayed());
} finally {
await driver.quit();
}
});
});Magic Link Login
const assert = require("assert");
const { Builder, By, until } = require("selenium-webdriver");
const { TrivumoClient } = require("trivumo");
describe("Magic Link Login", function () {
this.timeout(120000);
it("logs in using a magic link", async function () {
const driver = await new Builder().forBrowser("chrome").build();
const trivumo = new TrivumoClient({
apiKey: process.env.TRIVUMO_API_KEY,
});
const email = await trivumo.generate_email();
try {
await driver.get("https://example.com/login");
await driver.findElement(
By.NAME("email"),
).sendKeys(email.address);
const requested_at = new Date().toISOString();
await driver.findElement(
By.CSS_SELECTOR("button[type='submit']"),
).click();
const message =
await trivumo.wait_for_message_after(
requested_at,
{
to: email.address,
subject: "Your magic login link",
},
);
const magic_link =
message.html.links[0].href;
await driver.get(magic_link);
const dashboard = await driver.wait(
until.elementLocated(
By.xpath("//*[contains(text(),'Dashboard')]"),
),
10000,
);
assert.ok(await dashboard.isDisplayed());
} finally {
await driver.quit();
}
});
});One-Time Password (OTP)
const assert = require("assert");
const { Builder, By, until } = require("selenium-webdriver");
const { TrivumoClient } = require("trivumo");
describe("OTP Verification", function () {
this.timeout(120000);
it("verifies an OTP sent by email", async function () {
const driver = await new Builder().forBrowser("chrome").build();
const trivumo = new TrivumoClient({
apiKey: process.env.TRIVUMO_API_KEY,
});
const email = await trivumo.generate_email();
try {
await driver.get("https://example.com/verify-otp");
await driver.findElement(
By.NAME("email"),
).sendKeys(email.address);
const requested_at = new Date().toISOString();
await driver.findElement(
By.CSS_SELECTOR("button[type='submit']"),
).click();
const message =
await trivumo.wait_for_message_after(
requested_at,
{
to: email.address,
subject: "Your verification code",
},
);
const otp_code =
message.html.codes[0] ??
message.text.codes[0];
await driver.findElement(
By.NAME("otp"),
).sendKeys(otp_code);
await driver.findElement(
By.CSS_SELECTOR("button[type='submit']"),
).click();
const success_message = await driver.wait(
until.elementLocated(
By.xpath(
"//*[contains(text(),'Verification successful')]",
),
),
10000,
);
assert.ok(await success_message.isDisplayed());
} finally {
await driver.quit();
}
});
});Validate Email Content
const assert = require("assert");
const { TrivumoClient } = require("trivumo");
describe("Email Content Validation", function () {
this.timeout(60000);
it("validates email subject, content, and links", async function () {
const trivumo = new TrivumoClient({
apiKey: process.env.TRIVUMO_API_KEY,
});
const email = await trivumo.generate_email();
const message = await trivumo.wait_for_message({
to: email.address,
subject: "Welcome",
});
assert.strictEqual(
message.subject,
"Welcome",
);
assert.ok(
message.html.body.includes(
"Getting Started",
),
);
assert.ok(
message.html.links.some(
(link) =>
link.href.includes("/onboarding"),
),
);
});
});Best Practice
When waiting for transactional emails that are triggered by a user action, prefer wait_for_message_after().
const requested_at = new Date().toISOString();
await driver.findElement(
By.CSS_SELECTOR("button[type='submit']"),
).click();
const message =
await trivumo.wait_for_message_after(
requested_at,
{
to: email.address,
subject: "Reset your password",
},
);Using wait_for_message_after() ensures only emails received after the triggering action are considered. This prevents false positives caused by previously received messages and makes tests more reliable.