How to Send Email notification of Test report in Selenium?

If we use continuous integration through Jenkins, then we can easily configure email notification through the pipeline. In that case, we do not require the coding practices. Here we are going to discuss the implementation of send email notification feature in our Selenium project. Therefore, the agenda of this tutorial is about sending test report through email notification. We will implement it through Java programming.

Why do we need to implement send email notification in Selenium?

There are some stakeholders who are not directly accessing our test repository, hence, they can’t access the test execution report directly. Therefore, it is always a good practice to implement this feature in our test framework so that all the stakeholders get notified with the test execution status which contains the test reports.

Pre-requisites for implementation of ‘send email notification’ feature in Selenium

There is some pre-requisite which a developer needs to follow before implementing this feature in the Selenium project.

  • Add JavaMail API JAR file in your Selenium project. Click here to download the JAR file.
  • Alternatively, you can create maven dependency of JavaMail API. Click here to get the Maven dependency of JavaMail API.
  • Get ready with SMTP (Simple Mail Transfer Protocol) hostname and port number

In addition to above Jar files, we need to import some of the Packages, which are listed as below:

Well, these packages are just the part of coding.

How to implement ‘Send email notification’ feature in the Selenium project?

Here are some of the steps which implement this email notification service in your automation project.

At first, we need to identify the list of recipients who are going to receive the email notification, then identify the sender’s email address with their SMTP and host’s port number. We will implement this service after the entire test execution ends so that it could trigger the mail with the segregated test reports. In the previous article, we discussed the technique to generate the PDF report of the test execution and in this article, we will learn the technique to send that PDF report through email. If you haven’t gone through the previous article, then I would like to suggest you one time reading of the tutorial. Click on the link below to read the tutorial of PDF report creation.

Next, we set the SMTP hostname as the property, further, we create a new session to implement the email service. After session set up, we create the instance of MimeMessage() class and we pass the instance of the session in the constructor. Further, we create Internet address of Sender and recipients.

After all these setups, we create the first message body in which we define the subject of the email and email’s body. In the second message body, we insert the test report as an attachment and finally, we combine these message body with MultiPart class.

In the end, we use Transport to authenticate the session and login validation. Once it is authenticated then the user successfully sends the email to the stakeholders.

Here is the coding implementation of the above explanation.

Email Output

Following screenshot shows the email sent from the sender’s mail address.

Send email notification of Test report in Selenium

That’s all about ‘send email notification of Test report in Selenium’. You can raise your queries through the comment form below and don’t miss to join our Facebook group for quick updates on Selenium.

Join Inviul fb group

Leave a Reply