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.

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 playground.mailslurp.com. 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 :

Install the dependencies with . 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.

Writing tests

Now time to write some tests. Create directories. We will use for this demo. Then create a test class:

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.

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

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

Load the playground site in selenium

First step: load the MailSlurp playground app.

Start the sign-up process

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

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.

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

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.

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.

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.

Test sign-in as confirmed user

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

When successful we will see a nice dog.

Conclusion

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.