Automated Email Testing with Java and Mailosaur

Tests related to emails are often ignored or left to manual testing, although test automation tools could do it efficiently. In this article, we’ll explain how to use the Mailosaur service in order to automate your email testing in a breeze.

Ranorex Webtestit
Ranorex Webtestit
8 min readNov 7, 2019

--

Emails continue to be the core functionality of many systems and an essential way to communicate with the users of our services. Sending registration links or password recoveries, as well as newsletters, reports or invoices to our customers is possible thanks to email. But only if our email workflow works.
Even the smallest layout or configuration error can cause our emails to fail, i.e. they may not display properly, they may not reach users, etc. If this happens too often, it can result in a loss of customer trust and interest in our products or services. This means losing business.
Of course, all these problems can be avoided if we test our email workflows regularly.

However, many still underestimate the importance of email testing, mainly because most of us believe that email will work error-free without any intervention.
Those who are little braver to dive into email testing usually resort to sluggish and costly manual testing, not because they like manual work, but because of the complexity of automating email tests. Email test automation is, let’s admit it, a major PITA, so most of us choose not to test this important part of our systems at all.
Luckily, there is a perfect solution to interact with email through code and automate email testing easily. It is called Mailosaur.

What is Mailosaur?

Mailosaur is a cloud-based service that allows us to capture and test our emails. Mailosaur provides an email box or lets you generate one on-the-fly, that can be accessed via Mailosaur’s interface or their API that is available in most programming languages.
Mailosaur uses real SMTP servers, along with an unlimited number of email addresses we can use for our tests.
Most importantly, Mailosaur integrates with any framework and provides client libraries (SDKs) for all major languages, including Java, Python, JavaScript, C#, etc.

Integrating Mailosaur with Ranorex Webtestit to Test Your Email Workflows

To see how email testing works, we’ll integrate Mailosaur to our testing framework — Ranorex Webtestit, a complete toolset for creating Selenium E2E tests with Java, TypeScript, and Python.

In this article, we’ll demonstrate how to perform email testing with Java. This project is available for download from our GitHub repository.
You can also use it as a reference to try email testing with Python or TypeScript, using Mailosaur and Ranorex Webtestit, which both support these languages.

After opening a new Java project, Ranorex Webtestit will scaffold a new Maven project. So, the first step would be to add the following dependency to your POM.xml:

<dependency>
<groupId>com.mailosaur</groupId>
<artifactId>mailosaur-java</artifactId>
</dependency>

We’ll test this simple subscription form. To do that, we need to enter an email address, click the ‘Submit’ button and get the message that we have subscribed successfully. This means that we need to add those three elements to our test using selectors for each.
Now, Ranorex Webtestit uses the Page Object pattern which is the recommended practice to easily organize selectors and handle elements for different web page components, such as headers, menus or content areas.
Since we have only one page here, we can go ahead and create a single Page Object named RegistrationPo.java with three elements in it. To do that, we’ll use the Ranorex Selocity extension that will help us create a Page Object in Ranorex Webtestit.

We will also use Ranorex Selocity to add web elements to the Page Object we just created.

The added elements will appear in the ‘Elements’ tab in Ranorex Webtestit with generic names. Rename them to emailField and submitButton.

To add the last element (message), type any email in the field and click the ‘Submit’ button. The ‘Thank you for subscribing!’ message will appear. Add it to Ranorex Webtestit like you added the previous two elements and rename it subscriptionSuccessText.
Finally, we’ll have three elements in our RegistrationPo Page Object file.

Now it’s time to add some emails in order to test this form. Enter Mailosaur.

To start testing with Mailosaur, we first need to create an account.
After creating an account, we’ll obtain our API key which is required in order to authenticate our API requests with Mailosaur. The API key is available in
‘Your Account’ > ‘API Access’ > ‘Your API Key.’

In addition to the API key, we’ll also need Server ID for this test. Mailosaur allows us to create multiple servers, for example, Live server, Staging server, and so on. Each server comes with a unique base email address available in the following form:

.SERVER_ID@mailosaur.io

When we want to send or receive an email using Mailosaur, all we need to do is type anything in front of our server name. For example, if our server name is ‘b0xsup5f’, we can name our emails like ‘test123.b0xsup5f@mailosaur.io’ or ‘just-testing-things.b0xsup5f@mailosaur.io’ and they will be sent directly to our server.
However, for this test, we want Mailosaur to generate random email addresses using our API key and Server ID.

Imagine the scenario in which we have to test our registration form, or more precisely the email sent to the user who registered for our service. We expect the email to contain some text and a single link leading to the users’ profile login dialog.

We will send the ‘Day 1: Project Kickoff!’ email generated by the Team Guru99 for testing purposes with the following content:

We will create two tests for these scenarios.

The first test will include the following steps:

1. Generate a random email address using the Mailosaur client
2. Use the generated email address to fill out the registration form
3. Confirm a successful subscription
4. Get the link text from the last received email
5. Assert that the link points to the right address

All steps except Step 4 will be included in the second test. Instead of getting link text, in the second test, we’ll try to get the confirmation link from the last received email that matches the search criteria we’ll define in our Page Object file.

But we need to prepare a few things in our RegistrationPo file before we can create and run our tests. First, we’re going to create some actions to work with the elements we added to the Page Object file.

Ranorex Webtestit allows you to simply drag and drop an imported element into the Page Object code to generate methods by selecting an action from a drop-down menu.

So, the first step will be to drag the three elements from the ‘Elements’ tab and select the action for each.
For emailField, choose ‘Do > Type into element’ from the drop-down menu. For submitButton, choose ‘Do > Click on element’, and for subscriptionSuccessText select ‘Get > Element’s text.’

That portion of code should look like this:

Then we’ll create a private instance for our Mailosaur client:

Next, we’ll enter our Mailosaur API and Server ID. While we could hardcode the credentials in code, Ranorex Webtestit allows us to pass custom properties to test executions and define these properties as environment variables. To do so, we’ll add the API key and Server ID into the default.endpoints.json file.

Of course, you need to add your actual API key and Server ID between the quotes.

We can easily access these properties with the following line of code in our Page Object file:

After that, we’ll instantiate the Mailosaur client by updating our POs constructor:

Then, using the Mailosaur Server ID, we will generate a random email address:

After that, we’ll set up things we want to test. First, we want to get the last email and return the registration link text from it.

In the second test, we want to get an email with specified criteria (the “Day 1: Project Kickoff!” subject) and return the registration link text. So, we’ll add the following line of code to our Page Object file:

After setting up things in our Page Object file, it’s time to create our tests. Right-click the ‘tests’ folder in the project tree and select the ‘New test file.’

We will name the first test RegistrationTest.java. The code should look like this:

After that, create the second test and call it RegistrationTestWithCriteria.java with the following code:

To run our tests, we need to add an endpoint to configure the browser, operating system, devices and other parameters relevant to our tests. The Mailosaur API and Server ID properties we entered before will be also stored in our endpoint file.
After saving an endpoint, you can proceed and run either a single test file or both test files at once.

Once the test run is completed, Ranorex Webtestit will generate a test report. If you followed all the steps correctly, both tests should execute without errors.

Also, all emails that you sent through these tests or using Mailosaur credentials elsewhere will also be available on the Mailosaur Server settings page.

You can learn more about all the cool Mailosaur features here.

Conclusion

We’ve demonstrated how you can easily automate email testing if you integrate Mailosaur client library with any testing framework.
Here, we used Ranorex Webtestit that makes creating tests for the whole email testing process a lot easier.

Mailosaur and Ranorex Webtestit are a perfect fit for email testing with Java, TypeScript or Python, which is no wonder, as both are helpful dinosaurs.

You can download a free, fully-featured trial of Ranorex Webtestit to start testing your emails right away.

--

--