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:
We will focus on most commonly used two methods;
Let's see how to implement in test automation framework,
<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("[email protected]", "password"));
email.setSSLOnConnect(true);
email.addTo("[email protected]");
email.setFrom("[email protected] ");
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") + "\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:
<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("[email protected]"));
message.addRecipient(javax.mail.Message.RecipientType.TO, new InternetAddress("[email protected]"));
message.setSubject("Email Subject");
message.setText("Email Body");
Transport.send(message);