Top

Test Automation Utility to Verify Email

Test Automation Utility to Verify Email

Many applications send its users emails as part of the process flow but it’s not obvious how to use Selenium WebDriver to verify email. This is of course possible, but automating UI steps of a 3rd-party email application is not the most optimal approach. Because of this, I worked on a project once where we simply skipped the verification of the email receipt. This always bothered me, so I eventually blocked out some time to research how I might be able to automate the verification of email.

I found the JavaMail API. This is an absolute life saver! With this, I’ve been able to not only verify that an email was received, but also verify its contents, open links within the email, extract data sent (temp passwords, verification codes, etc). Someone recently asked me about email verification within automation, so I decided to open source the utility methods I’ve written that use the JavaMail API.

This is written to work for Gmail accounts. I’ve also tweaked this in past projects to work for Outlook accounts as well, so that’s definitely possible.

Downloading the Dependencies

Here are the dependencies that I add to my maven project. These go inside of pom.xml

 

Setting Email Properties

There’s quite a few properties that need to be set. It’s cleaner to put this in a properties file as opposed to directly in the code, but that would work just as well. Here’s my email.properties file which is located in a resources folder in my project.

 

Utility Class

All interactions with the email application are contained in this EmailUtils.java class. Tests can then call any method needed.

I start by defining a private Folder object which represents the current folder that we’re reading from. I also created an enum of possible folders my application’s emails may be found in. This can be expanded to any other folders that exist in the email application.

 

Next, I provide multiple constructors. All of these may not necessary in most cases, but I wanted to be as flexible as possible to allow for the use of another properties file that holds the email account authentication data, and also provide a constructor that allows you to just pass the data in.

If you’d like to store the email credentials in a properties file, you can add methods to be able to retrieve them. These are needed for the second constructor.

Here are methods to interact and/or read info from the email folder

 

Boolean methods helpful for verification

 

Parsing methods can also prove helpful. For example, if your application sends out an authorization code that you need to retrieve to verify or continue on with your testing, you can use the method below to parse it out. Here’s an example where the data you want to parse is on the same line with other text.

Example Email

Subject: Authorization CodeDear JANE DOE:

The authorization code you requested is listed below.
Authorization code:870632
Please note that this code is temporary. If you try entering the authorization code and it has expired, you’ll need to request a new one.

 

 

There could also be the case where the data you need to parse is on its own line. In that case, the method would be slightly different.

Subject: Authorization CodeDear JANE DOE:

The authorization code you requested is listed below.
Authorization code:
870632
Please note that this code is temporary. If you try entering the authorization code and it has expired, you’ll need to request a new one.

 

 

EmailUtils.java in its entirety

 

Using the Utility in Tests

All of the following methods are example test methods that would go in another class, such as EmailTests.java

Connect to Email

 

Example of test using verification code

 

Example of test verifying that email contains a specific text

Notice the call to getMessagesBySubject. This utility method allows you to indicate whether you only want to get unread messages or not. This is very helpful for tests that are running repeatedly. This ensures you’re not looking at an older message from a previous run.

The third argument here, 5, is the max number of messages to look through to find this email message. This also helps ensure you’re only looking at the latest messages, and also speeds up the test by limiting the search and avoiding going through the entire folder.

getMessagesBySubject returns an array of Message objects, so the test uses an index to get the first one (which would be the latest one in the email folder)…the one we want.

 

Go to link within email

The utility method getUrlsFromMessage will return a list of links contained within the email. This method can of course be used to verify the link’s existence, but coupling this with Selenium WebDriver, you can also go to the link if you need to do further verification.

 

There are many more methods in the EmailUtils class that can prove useful for your automation needs. The utility class is on Github, so can be downloaded and/or contributed to.

 

Troubleshooting

Gmail has gotten stricter about third party applications accessing their services. I’ve noticed recently, Gmail will block the authorization and you’ll see an error like this

Please log in via your web browser and then try again

 

Additionally, you may also get an email from Gmail alerting you that the API is trying to access your account and has been blocked.

To resolve this, you’ll need to change your settings within Gmail to allow access to less secure accounts. Note that this makes the account more susceptible to attacks, so I wouldn’t recommend doing this on your corporate or even personal account. Ensure you’re using an account set up just for testing which holds no sensitive data.

Angie Jones
4 Comments
  • Alister Scott

    We use a service called Mailosaur which built straight for this purpose and allows full API access to all emails and JSON objects to easily retrieve content. We found using GMail too unreliable due to Google constantly blocking and shutting down the account for API access. See my blog post: https://watirmelon.blog/2016/02/15/testing-email-in-e2e-tests-with-mailosaur/

    January 29, 2018 at 10:07 pm Reply
  • Jithin Mathew

    Hi Angie, Nice tutorial.

    I have a suggestion. A disposable web mail applications will also do the same job. We can get the message content by using their API’s. This one is an open source.

    http://www.inbucket.org.

    January 30, 2018 at 11:26 am Reply
    • Greg

      Thanks for recommending inbucket. I’ll try it out since Gmail sometimes messes up my automation workflow with their random verification of recovery info.

      February 16, 2018 at 11:48 am Reply
  • Luke

    I use Putbox.com which allows me to send an email to anyemail@putbox.com then hit the website itself to grab the email and get/verify whatever I need in it. It’s a nice simple, fast, and free option for verifying email.

    April 20, 2018 at 9:20 pm Reply

Post a Comment