Intelligent Reporting of Automated Test Execution
Intelligent Reporting of Automated Test Execution

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:

  • Add below dependency in pom.xml file

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-email</artifactId>
    <version>1.5</version>
</dependency>        

  • Various instances of email classes can be created (HtmlEmail, SimpleEmail, and MultiPartEmail)
  • Then set the necessary properties for your email, such as the sender's email id, recipient's email id, subject, and message content.

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("[email protected]", "password"));
		email.setSSLOnConnect(true);
		email.addTo("[email protected]");
		email.setFrom("[email protected] ");
		email.setSubject("email Subject");        

  • Apache Commons Email allows to attach files and send HTML-formatted emails easily.
  • send method used to send the email.

email.setHtmlMsg("<html><h1>Test Execution Report</h1></html>");

//Create the attachment
EmailAttachment attachment = new EmailAttachment();
attachment.setPath(System.getProperty("user.dir") + "\src\test\resources\com.Reports\ZipFileToSend.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:

  • Add below dependency in pom.xml file

<dependency>
    <groupId>com.sun.mail</groupId>
    <artifactId>javax.mail</artifactId>
    <version>1.6.2</version>
</dependency>        

  • Set up different properties such as email address, subject, reply to email, email body and attachment.
  • SMTP server is used to send emails. JavaMail API supports both TLS (Transport Layer Security) and SSL (Secure Sockets Layer) authentication for sending emails.
  • Replace SMTP Server and SMTP Port number with your actual details.

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");         

  • Create a Session object using Session.getDefaultInstance(props, auth);.
  • The Session will be used to create the email message and manage the email session.
  • Provide valid sender's email id as username and senders password in password.

Session session = Session.getInstance(props,
         new javax.mail.Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
               return new PasswordAuthentication(username, password);
	   }
         });        

  • Compose the email - Create a MimeMessage object to represent the email message and set the sender email id, recipient email id, email subject and body.
  • Transport.send() method is used to send mail.

MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress("[email protected]"));
message.addRecipient(javax.mail.Message.RecipientType.TO, new InternetAddress("[email protected]"));
message.setSubject("Email Subject");
message.setText("Email Body");

Transport.send(message);        






要查看或添加评论,请登录

Verve Square Technologies的更多文章

社区洞察