Automated Test

Intelligent Reporting of Automated Test Execution

It is always important to share test execution results and log details after test execution. Doing this manually post every execution is time consuming and causes delays. Automating the email process with all test results details saves time, keeps everyone updated quickly and helps the team work together better.

Jenkins and other CI/CD tools can send email automatically using plugins. This article shows how to integrate email utility with selenium java test automation. This helps send emails automatically with test results, even when running tests on a local computer.

There are different ways to automate emails after Selenium Java tests:

  1. Email library
  2. JavaMail  API
  3. Maven Plugins
  4. Third Party Services/API

We will focus on most commonly used two methods;

  1. Email library: Apache Commons Email is a robust library that simplifies email sending. It is a popular and user-friendly library that simplifies the process of sending email. It provides a high-level, easy-to-use API for sending emails via various protocols, including SMTP.
  2. JavaMail API: JavaMail API is used to send emails using SMTP server. JavaMail API supports both TLS and SSL authentication for sending emails. It provides classes and interfaces to send, receive, and manage email messages.

Let’s see how to implement in test automation framework,

  1. Apache commons Email Library:
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-email</artifactId>
    <version>1.5</version>
</dependency>

Here we are using HtmlEmail

import org.apache.commons.mail.DefaultAuthenticator;
import org.apache.commons.mail.EmailAttachment;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.HtmlEmail;
import org.apache.commons.mail.MultiPartEmail;
HtmlEmail email = new HtmlEmail();
		email.setHostName("smtp.zoho.com");
		email.setSmtpPort(587);
		email.setAuthenticator(new
DefaultAuthenticator("sender@mail.com", "password"));
		email.setSSLOnConnect(true);
		email.addTo("receiver@mail.com");
		email.setFrom("sender@mail.com ");
		email.setSubject("email Subject");
email.setHtmlMsg("<html><h1>Test Execution Report</h1></html>");
//Create the attachment
EmailAttachment attachment = new EmailAttachment();
attachment.setPath(System.getProperty("user.dir") + "srctestresourcescom.ReportsZipFileToSend.zip");
attachment.setDisposition(EmailAttachment.ATTACHMENT);
attachment.setDescription("set_description");
attachment.setName("name_to_the_file");
email.attach(attachment);
//to Send email
email.send();

2. JavaMail API:

<dependency>
    <groupId>com.sun.mail</groupId>
    <artifactId>javax.mail</artifactId>
    <version>1.6.2</version>
</dependency>
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;
Properties props = new Properties();
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.host", "SMTP Server"); 
        props.put("mail.smtp.port", "SMTP Port number");
Session session = Session.getInstance(props,
         new javax.mail.Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
               return new PasswordAuthentication(username, password);
	   }
         });
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress("sender@mail.com"));
message.addRecipient(javax.mail.Message.RecipientType.TO, new InternetAddress("receiver@mail.com"));
message.setSubject("Email Subject");
message.setText("Email Body");
Transport.send(message);

Tags:
Automated Test
Share:

Linkedin


Twitter


Youtube

Related Post


edit post


Test Automation Streamlining REST API Processes with Postman Automation Summary:In today’s world, industries are increasingly focusing on the development of…

Read More


edit post


Automated Test, Uncategorized Enhancing Test Efficiency with Playwright, TestNG and Allure Summary:Playwright is an open-source library developed by Microsoft for…

Read More


edit post


Test Automation Continuous Integration and Delivery with Jenkins and GitHub Summary: Testing and deployment-related operations can be automated with Jenkins,…

Read More


edit post


Mobile App Test Automation with TDD Telecommunications (Cable) Location Performance Testing CONTEXT Elyments is an India-based mobile application similar to…

Read More