How to test email verification with Selenium

Stop people being blocked out of accounts new and old.

Generic AI Artwork

When people create new accounts, it’s normal for you to need a way of contacting them with onboarding information, billing alerts, and more. That’s why having the right email address is so important.

However, what if they made a typo when providing the email address? Or they simply want to create an account without any real interest in using the service as intended? In either case you’ll want to verify that they can access the email address they’ve given you.

The simplest approach adopted by most is to send a unique link in an email to that address, and have a user click on that link to verify their account. It’s simple, but it works.

But how can you test this kind of workflow with Selenium?

Why can’t Selenium test this already?

While Selenium is an extremely capable tool for testing web content and automating user journeys, it was never designed for capturing and evaluating emails on its own. At best, you could navigate to a webmail client like Gmail and try to pull content out that way, but this is time consuming, error prone, and limits the number of email addresses you have easy access to. This has the effect of limiting and skewing results and can lead to issues going unnoticed.

So, what often happens is testers manually test their email verification process on a regular basis instead, which is significantly less robust and means it’s easy to miss a regression caused by the simplest of code changes elsewhere in the product or service.

Using Selenium for account verification testing

By adding a tool like Mailosaur to Selenium, it's possible to run the tests you need. It easily integrates with Selenium, giving you the ability to automate email tests with an unlimited number of test email addresses, so you can effectively check email verification processes, however you choose to do it.

Run tests to check any link sent to an email works as intended, which you can use for account creation, or closure processes. Whatever works for you. In the same vein, if you choose to send a verification code in your email, it’s easy to extract it for testing purposes within Selenium, or other existing test frameworks, as well.

Running these tests is simple. Here’s a quick example of such a test:

String apiKey = "API_KEY"; 
String serverId = "SERVER_ID"; // The unique ID of the inbox (server) that you are using 
 
// Create a MailosaurClient instance, using your API key 
MailosaurClient m = new MailosaurClient(apiKey); 
 
// Random test email address (this uses a catch-all pattern)
String randomString = UUID.randomUUID().toString().replaceAll("-", "").substring(7, 13);
String emailAddress = randomString + "@" + serverId + ".mailosaur.net";

// 1 - Sign up (to trigger a verification email)
browser.get("https://example.mailosaur.com/signup");
browser.findElement(By.cssSelector("input#email")).sendKeys(emailAddress);
browser.findElement(By.cssSelector("input#password")).sendKeys("my password");
browser.findElement(By.cssSelector("button[type='submit']")).click();

// 2 - Create the search criteria for the email
MessageSearchParams params = new MessageSearchParams();
params.withServer(serverId);

SearchCriteria searchCriteria = new SearchCriteria();
searchCriteria.withSentTo(emailAddress);

// 3 - Get the email from Mailosaur using the search criteria
Message msg = m.messages().get(params, searchCriteria);

assertEquals("Verify your ACME Product account", msg.subject());

// 4 - Extract the link from the email
String verificationLink = msg.html().links().get(0).href();

// 5 - Navigate to the link to verify the address
browser.get(verificationLink);
assertEquals("Your account is now verified!", browser.getTitle());

browser.quit();

Links

The crucial part here is the last part; the link or verification code inside the email itself. If it doesn’t work, your account verification process as a whole won’t work. Whether you choose a link or a verification code, Mailosaur can automatically detect and extract what you need, making it available for navigation with Selenium.

String verificationLink = msg.html().links().get(0).href();

browser.get(verificationLink);
assertEquals("Your account is now verified!", browser.getTitle());

Try it out

Assuming you’re an existing Selenium user, the best way to find out if Mailosaur is right for you is by giving it a go. To help with this, you can start a 14-day free trial. And if there’s more you’d like to know about Mailosaur, with or without Selenium, you can speak to our sales team, or try out our documents page.