Java TestNG Selenium user sign up testing with real email addresses

Testing web app user sign up with TestNG and MailSlurp: a Java-based end-to-end test suite using real email addresses for authentication.

  • Table of contents


TestNG is a popular Java test framework that can be used with Selenium and MailSlurp to test web applications end to end. In this example we will use MailSlurp test email accounts to test a demo application sign up process end to end using real email addresses.

sign up

The full code for the example can be found on GitHub. See the MailSlurp JavaDocs site for more information.

Demonstration web app

For this example we will use a React App hosting at The app has a typical user sign authentication flow:

  • user signs up with an email address and password
  • a confirmation code is sent to the user by email
  • user confirms their account by entering the code
  • user logs in and sees a welcome screen.

We will use MailSlurp to create a test email account for the user and sign-up using Selenium. A successful sign-up and login results in a happy dog being displayed to the user.

Scaffolding a TestNG Java test suite

Create a new directory and add the following pom.xml:

<project xmlns="" xmlns:xsi=""

Install the dependencies with mvn install. This will install TestNG, Selenium, and MailSlurp. MailSlurp is free to use - you just need to an API Key. Get an API Key from the MailSlurp dashboard by creating an account.

api key

Writing tests

Now time to write some tests. Create src/main/java/com.yourpackage directories. We will use com.mailslurp.examples for this demo. Then create a test class:

package com.mailslurp.examples;

public class SignUpTestNGExample {}

Configure webdriver and Selenium

Selenium requires a webdriver to run tests in a browser. We will use a Makefile to download a FireFox (Geckodriver) webdriver and pass the location to our tests with an environment variable.

-include ../.env


	curl -s -L "$(DRIVER_URL)" | tar -xz
	chmod +x $(DRIVER_LOCATION)

	mvn install test

Run make geckodriver to install the driver locally on a linux machine. Next let's set up a @BeforeSuite method to initialize the driver with selenium.

// website useful for testing, has a real authentication flow
private static final String PLAYGROUND_URL = "";

// get a MailSlurp API Key free at
private static final String YOUR_API_KEY = System.getenv("API_KEY");
private static final String WEBDRIVER_PATH = System.getenv("PATH_TO_WEBDRIVER");
private static final String TEST_PASSWORD = "password-" + new Random().nextLong();
private static final Boolean UNREAD_ONLY = true;
private static final Long TIMEOUT_MILLIS = 30000L;
private static Inbox inbox;
private static Email email;
private static String confirmationCode;
private static ApiClient mailslurpClient;
private static WebDriver driver;

public void initTestSuite() {

    // setup mailslurp
    mailslurpClient = Configuration.getDefaultApiClient();

    // setup webdriver (expects geckodriver binary at WEBDRIVER_PATH)
    assertTrue(new File(WEBDRIVER_PATH).exists());
    System.setProperty("webdriver.gecko.driver", WEBDRIVER_PATH);
    driver = new FirefoxDriver();
    driver.manage().timeouts().implicitlyWait(TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);

Let's also add an @AfterSuite method to close the webdriver when finished:

@AfterSuite(alwaysRun = true)
public void tearDown() {

Load the playground site in selenium

First step: load the MailSlurp playground app.

public void test1_canLoadAuthenticationPlayground() {
    assertEquals(driver.getTitle(), "React App");

Start the sign-up process

Click the sign up button to begin a user sign up.

public void test2_canClickSignUpButton() {

Create a real email address with MailSlurp and use it to start sign-up on the playground

Now we can create a test email address for the user using MailSlurp and sign up with it in the Playground.

submit code

public void test3_canCreateEmailAddressAndSignUp() throws ApiException {
    // create a real, randomized email address with MailSlurp to represent a user
    InboxControllerApi inboxControllerApi = new InboxControllerApi(mailslurpClient);
    inbox = inboxControllerApi.createInbox(null, null,null, null,null,null,null, null, null);

    // check the inbox was created

    // fill the playground app's sign-up form with the MailSlurp
    // email address and a random password

    // submit the form to trigger the playground's email confirmation process
    // we will need to receive the confirmation email and extract a code

Use MailSlurp to receive the confirmation email that is sent by playground

submit code

You can wait for the confirmation email to arrive for the user by using the WaitForControllerApi methods. We expect the user's email address to receive a confirmation code.

public void test4_canReceiveConfirmationEmail() throws ApiException {
    // receive a verification email from playground using mailslurp
    WaitForControllerApi waitForControllerApi = new WaitForControllerApi(mailslurpClient);
    email = waitForControllerApi.waitForLatestEmail(inbox.getId(), TIMEOUT_MILLIS, UNREAD_ONLY);

    // verify the contents
    assertTrue(email.getSubject().contains("Please confirm your email address"));

Extract the confirmation code from email body using regex patterns

We can use a regex pattern to extract the verification code from the email body.

public void test5_canExtractConfirmationCodeFromEmail() {
    // create a regex for matching the code we expect in the email body
    Pattern p = Pattern.compile(".*verification code is (\\d+).*");
    Matcher matcher = p.matcher(email.getBody());

    // find first occurrence and extract
    confirmationCode =;

    assertTrue(confirmationCode.length() == 6);

Submit the confirmation code to the playground to confirm the user

Now that we have the confirmation code we can use Selenium to submit the code.

submit code

public void test6_canSubmitVerificationCodeToPlayground() {

Test sign-in as confirmed user

For the final step sign in using the email address and password that we verified.

public void test7_canLoginWithConfirmedUser() {
        // load the main playground login page

        // login with now confirmed email address

        // verify that user can see authenticated content

When successful we will see a nice dog.

submit code


TestNG is a great framework for testing end-to-end with Selenium and Java. You can use MailSlurp to create real test email accounts. You can test user actions like user sign-up and account verification using real email addresses. Try it out for free today.

Find the full code on example.

Email and SMS Platform
Create a free account in 3 clicks