Selenium is a powerful browser testing framework. This article will show you how to use real email addresses in Selenium tests - no mail server required.

Introduction

selenium email example

Many web applications rely on email: for user sign-up, password reset, newsletters, support responses and more.

This presents a challenge for QA testers using Selenium: how do we test email related processes? Well with free test email account APIs like MailSlurp we can create email addresses within tests and control them from code.

Let's see an example.

The source code for this example is available on GitHub. See the docs for a Java language guide.

Example usage

For this example let's write a Selenium test to sign a user up to a dummy web application.

The test app

We'll test against playground.mailslurp.com for this example, a simple React application with a user sign-up process.

welcome

Sign up process

The playground allows a user to sign-up with an email address and password. The app then sends them the user an email containing a confirmation code. The user must copy the code and submit it to the app to confirm their account.

Once confirmed the user can access a picture of a dog by logging in!

welcome

We will test this process end to end.

Test process

We use Selenium to load the playground login form in Firefox. Then we use MailSlurp to generate an email address. Then we enter the email address into the sign up form, receive the welcome email with MailSlurp, extract the verification code and confirm the user. We'll go over each step with examples.

First let's setup the project to include Junit, Selenium, and the MailSlurp Java SDK.

Maven setup

MailSlurp is now published to Maven central

Create a like so:

Gradle setup

Or if you use Gradle create a like so:

Import classes

You can import MailSlurp under

Download WebDriver

Next you need to download webdriver for your platform. We prefer GeckoDriver for automating Firefox but you can use ChromeDriver with Chrome or any other if you prefer.

Configure Selenium

Now we can create a test and configure Selenium and MailSlurp. You need an API Key to use MailSlurp but it is free for personal use. Get an API Key here then create a test like this:

The test

Now we can write some tests.

Load the application

Let's load the application and assert that the title is as we expect.

To run the test run . The result looks like this:

selenium email example

Start sign-up

Now let's click the sign-up button and load the sign-up page.

Create the an email address

Next we will create a new email address using the MailSlurp client.

The result should look like this:

selenium email example

Receive confirmation email

Now we can use MailSlurp to receive the confirmation email that is sent by playground.

Then we can extract the confirmation code from email body using regex pattern:

Confirm the user and login

Now that we have the confirmation code we can submit the code to the Playground and then login with a confirmed account. If successful we can verfiy the welcome message.

confirm user

confirm user

Next steps

This article showed how to test email related processes like user sign-up in Selenium with Java and MailSlurp. In this test we used them to create a new test email account with a unique email address. we then used the email address to sign-up a user and receive a confirmation code. MailSlurp is free for personal use and has bindings for several popular languages. You can use it to create email addresses on demand then send and receive emails and attachments in code. Check it out.