gmail

Gmail + IMAP – Automated testing solution for emails

Posted on Posted in Functional Testing, Software Testing, Test Automation

Test automation around emails is a pain, if not done properly.

Almost all the user facing web applications require user registration with the email and email usually serves as the username in the system – which should be unique.

There are two main problems in automating email functionality –

  • Generating unique email addresses
  • Accessing inbox and content of the specific emails to check correctness of content and delivery

When I discussed this with the evil tester, he pointed me to his blog post on getting disposable email addresses. When I followed the link to Lifehacker, from his blog post, the first problem was solved.

It was easy enough to generate a random number such as current time stamp and amend it to any Gmail address by adding a plus sign to create new email addresses which will go to the same inbox. So example @ gmail.com, example+123 @ gmail.com and example+321 @ gmail.com are all the same email addresses from Gmail’s point of view and unique for applications. This simple technique gave me an incredibly easy way to generate unique email addresses which can be accessed from a single inbox.

The second problem was a bit tricky to solve and extremely wrong way to solve this would be to automate functionality around Gmail UI to access the mail we need. This approach would result in an extremely slow and brittle test which will break because of changes in Gmail interface.

Fortunately, Gmail supports IMAP and allows different devices/browsers to access emails using IMAP. Also, the language we were using for the automation, Java has an excellent API to access emails using IMAP – JavaMail.

So the problem was solved by using Gmail’s trick to generate unique email addresses during every test run and then accessing inbox using Java mail API.

Let me give you a bit more details on how Java mail API can be used to access Gmail inbox using IMAP.

The first thing we needed to do was to access emails from Gmail inbox is connect to the store which models message storage and it’s access protocol. Steps to connect to the store are

  • Create an object of properties and define which protocol will be used
  • Create an email session by passing this property – session class in Java represent a mail session.
  • Now get the store which implements user’s desired protocol.
  • After getting the store object, it’s easy to connect to a particular email inbox using right credentials.

Properties props = System.getProperties();
props.setProperty("mail.store.protocol", "imaps");
Session session = Session.getDefaultInstance(props, null);
store = session.getStore("imaps");
store.connect("imap.gmail.com", USER_NAME, PASSWORD);

After connecting to the store, it was very easy to access emails from the inbox. To ensure that we get the right email easily, we have created a rule in Gmail to send emails to a specific folder if they originate from our known system email address.

Store object makes it incredibly easy to access a particular folder in a specific mode – all we need to do is access the folder and open in the appropriate way. It might be useful to open in read_write mode so that we can delete the emails after the test.

Folder testEmailFolder = store.getFolder(FOLDER);
testEmailFolder.open(Folder.READ_WRITE);

After opening the folder, it’s easy to access emails of a given folder and get relevant details from the message.

MimeMessage recentMessage = (MimeMessage) testEmailFolder.getMessage(1);
recentMessage.getSubject()

Some of the basic checks you can perform after getting message are

  • Is message delivered to the right user?
  • Does it have the right subject line?
  • Does it have required content (images/texts etc.)?

So that was fun – and hope you find it useful in your test automation efforts around emails. Do let me know if you have any opinion about this solution or if you have solved a similar problem in a different way. Also, if you find it useful, please share it in your network by tweeting, liking, or sharing on the Linked-In.

Please follow and like us: