MAILER AUTOMATION THROUGH JAVAMAIL API

0.00 avg. rating (0% score) - 0 votes

MAILER AUTOMATION THROUGH JAVAMAIL API

“Discovering the unexpected is more important than confirming the known.”

Owing to the large number of mails that are a part of the daily user interactions, it becomes imperative that the mails that reach the users are bug free. This system is developed as an initiative to continuously validate the mails that the end user is receiving for accuracy, without manually testing the mails each and every day.

Problem Statement: In Naukri, automating mailers was considered unfruitful mainly due to the low ROI yielded when tackled with conventional automation methods. Gmail, Yahoo, Hotmail, or any other mail service provider for that matter, are third party supports. The conventional method would have required using their code (particularly xpaths) for automation. The major difficulties faced were –

  • The third party codes are subject to change beyond our control. In fact, the process is quite frequent. This would have increased the turnaround time for manually handling the automation scripts.
  • The Xpaths you find for Gmail pages are more often unreachable when using selector options. One will have to use the positional path expressions (the ones that are obtained by the location of an element on a webpage).

Kr1

So, for example, accessing the encircled element would require you to create the following Xpath-

html/body/div[7]/div[3]/div/div[1]/div[4]/div[1]/div[1]/div[3]/div/div/div/form/fieldset[2]/div/div/div[2]/table/tbody/tr/td/table/tbody/tr/td/div/input[2]

Solution: The JavaMail API is a set of abstract APIs that model a mail system. The API provides a platform independent and protocol independent framework to build Java technology based email client applications. The JavaMail API provides facilities for reading and sending email. Service providers implement particular protocols. Several service providers are included with the JavaMail API package.

  • With the help of JavaMail API, we can connect to the user’s mailbox by providing the username and the password. Once the connection has been made, the desired email can be accessed and manipulated.

JavaMail will work in any browser that supports the required JDK version.

Pre-requisites Just add JavaMail API jar and Java Activation jar to the list of external jars provided to Selenium; and you are good to go with using all the necessary functionalities.

 PROCEDURE FOLLOWED:-

  1. Connect to the mailbox: The following code snippet is all that is needed to access the user’s mailbox –

                 Properties props = new Properties();

                 props.setProperty(“mail.store.protocol”, “imaps”);

               Session session = Session.getInstance(props, null);

               Store store = session.getStore();

               store.connect(“imap.gmail.com”, UserEmail, UserPassword);

               Folder selectedFolder = store.getFolder(FolderName);

               selectedFolder.open(Folder.READ_WRITE);

 IMAP or Internet Message Access Protocol is an Internet standard protocol used by e-mail clients to retrieve e-mail messages from a mail server over a TCP/IP connection.

UserEmail, UserPassword, FolderName are the variables whose value have to be provided by us. The freedom to select the FolderName gives an added advantage to select the folder to be searched at our own discretion.

  1. Search for the mail to be tested: This is the most crucial part as the correctness of the automated test cases totally depends on the search algorithm. The following points were kept in mind while designing the algorithm –
  • In Naukri, two or more mails may have the same sender’s address. Keeping this in mind, the mails were searched for the given subject line, which maintained their uniqueness.
  • Now considering the aspect that the number of mails with a given subject line may be greater than one. To handle this, we make sure that whenever our code accesses a mail, it is marked as read, using –

selectedFolder.getMessage(msgindex).setFlag(Flags.Flag.SEEN , true);

kr2

The Search algorithm is designed by implementing both the above                          conditions:-

If ((Message.GetSubjectLine).equals(SubjectLine) &&

Message.getFlag(Flags.Flag.SEEN )==true)

MailPresent = true;                                                     

  1. Extract the HTML and CSS content of the Email to be tested: Our main focus lies on visibility and functionality of the mail body that is reaching the end user. The following code extracts the desired content of the mail body –

     Message msg = selectedFolder.getMessage(msgindex);

     Object content = msg.getContent();

     if (content instanceof Multipart)

       {

         Multipart mp = (Multipart) content;

         for (int i = 0; i < mp.getCount(); i++)

           { BodyPart bp = mp.getBodyPart(i);

             if (Pattern

             .compile(Pattern.quote(“text/html”),

             Pattern.CASE_INSENSITIVE)

             .matcher(bp.getContentType()).find())

             writeToFile.writeText((String) bp.getContent() , StorageFileLocation);

           }

       }

 This HTML content retains all the functionality that the original mail contains, for example, auto login, links, attachments etc. All we have to do is to store this content on the local machine as an HTML file (.html extension). This file can be opened in a browser, and voila, we can carry the functional testing of the mail by our current knowledge skills.

kr3

The test cases, parameters and variables vary in accordance to the mail that is being tested.

Test Scenarios: As already stated, the test scenarios vary with the mail. But some of the common yet useful cases can be-

  • Testing whether the mail is being fired.
  • Testing the sender’s address of the mail.
  • Detecting the presence of attachments in the mail.
  • Testing the name and formats of attachments sent.
  • Verifying the date and time of the mail.
  • Finding the mailbox folder to which the mail is sent.
  • Verifying the visibility and functionality of crucial links.

Recently we encountered a bug where the mails that were being sent to the user were exposing the adaemon thread in the sender’s address, or some of the mails were reaching with null body content. The mailer automation suite is self-sufficient to report likewise at the earliest without any manual intervention.

With this new addition, it is now possible for us to increase our scope of automation to include the mails and reduce the manual efforts needed. Although it is in its initial phase of implementation at Naukri, it has provided solution to most of the issues we faced in the particular field, and in the long run, is sure to benefit the quality. I hope this article proves to be useful to your projects.

             “Keep innovating, Keep automating.” J

One thought on “MAILER AUTOMATION THROUGH JAVAMAIL API

Comments are closed.