Videos
Check out our tutorial video series.
Using MailSlurp with Junit and Selenium for Real Email Addresses in Selenium Tests - No Mail Server Required
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.
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.
For this example let's write a Selenium test to sign a user up to a dummy web application.
We'll test against playground.mailslurp.com for this example, a simple React application with a user 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!
We will test this process end to end.
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.
MailSlurp is now published to Maven central
Create a like so:
Or if you use Gradle create a like so:
You can import MailSlurp under
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.
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:
Now we can write some tests.
Let's load the application and assert that the title is as we expect.
To run the test run . The result looks like this:
Now let's click the sign-up button and load the sign-up page.
Next we will create a new email address using the MailSlurp client.
The result should look like this:
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:
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.
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.
Check out our tutorial video series.
Email and SMS guides for automation and testing.
View github project code for multiple languages.
Latest posts from the MailSlurp team.
Test, build, and automate messaging with a free MailSlurp account.