How to send a report through email using Selenium Webdriver?
Last Updated :
21 Aug, 2024
In the world of automation testing, generating and sharing test reports is crucial. After executing tests using Selenium WebDriver, it's often necessary to share the results with team members or stakeholders. Automating the process of sending these reports via email can save time and ensure that everyone stays informed about the testing outcomes.
In this article, we'll explore how to send a report through email using Selenium WebDriver, breaking down the steps and providing a complete program for easy implementation.
Sending Report Through Email in Selenium WebDriver
1. Set Up Your Environment
Before you start, make sure you have the necessary dependencies in your project. You'll need Selenium WebDriver for your test automation and JavaMail API to handle email sending. You can include these in your Maven project using the following dependencies:
XML
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.8.0</version>
</dependency>
<dependency>
<groupId>javax.mail</groupId>
<artifactId>javax.mail-api</artifactId>
<version>1.6.2</version>
</dependency>
2. Create the Test Report
Execute your Selenium tests and generate the test report. You can use tools like TestNG or JUnit to create reports. These reports are typically in HTML or XML format. Ensure the report is saved in a known location as you'll need to attach it to your email.
3. Set Up JavaMail API
The JavaMail API allows you to send emails programmatically. You'll need to configure the SMTP server settings (like Gmail, Yahoo, etc.) to send emails. Here’s a simple setup using Gmail:
Java
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "587");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
4. Authenticate the Email Account
You'll need to authenticate the email account you're using to send the report. This is done using the Session object in JavaMail API:
Note: This is not the password by which you login to gmail, instead you need to generate an app password from your Google Account's Security settings. Link.
Make sure you used the right path of report file.
Java
Session session = Session.getInstance(props, new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("[email protected]", "your_password");
}
});
5. Compose the Email
Now, you can compose the email by setting the subject, recipients, and body. You can also attach your test report to the email:
Java
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("[email protected]"));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("[email protected]"));
message.setSubject("Automated Test Report");
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText("Please find the attached test report.");
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
// Attach the report
messageBodyPart = new MimeBodyPart();
String filename = "path_to_report/report.html";
DataSource source = new FileDataSource(filename);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(filename);
multipart.addBodyPart(messageBodyPart);
message.setContent(multipart);
Transport.send(message);
System.out.println("Email sent successfully.");
} catch (MessagingException e) {
e.printStackTrace();
}
6. Run the Program
After setting up the email-sending code, integrate it with your test automation framework. You can call this email-sending function at the end of your test execution to automatically send the report.
Complete Program to Send Reports Through Email in Selenium WebDriver
Here's a complete example program that ties everything together:
Java
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
public class SendEmailWithReport {
public static void main(String[] args) {
// Set up the SMTP server settings
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "587");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
// Authenticate the email account
Session session = Session.getInstance(props, new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("[email protected]", "your_password");
}
});
try {
// Compose the email
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("[email protected]"));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("[email protected]"));
message.setSubject("Automated Test Report");
// Email body text
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText("Please find the attached test report.");
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
// Attach the report
messageBodyPart = new MimeBodyPart();
String filename = "path_to_report/report.html";
DataSource source = new FileDataSource(filename);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(filename);
multipart.addBodyPart(messageBodyPart);
// Combine message parts
message.setContent(multipart);
// Send the email
Transport.send(message);
System.out.println("Email sent successfully.");
} catch (MessagingException e) {
e.printStackTrace();
}
}
}
Output:
Email sent successfully.
Conclusion
Sending test reports through email is an essential part of the automation testing process. By following the steps outlined in this article, you can seamlessly integrate email reporting into your Selenium WebDriver projects. This automated process not only saves time but also ensures that your team stays up-to-date with the latest test results.
Whether you're using Gmail or another email service, the JavaMail API offers a flexible and reliable way to send reports programmatically.
Similar Reads
How to Take a Screenshot in Selenium WebDriver Using Java?
Selenium WebDriver is a collection of open-source APIs used to automate a web application's testing. To capture a screenshot in Selenium, one must utilize the Takes Screenshot method. This notifies WebDriver that it should take a screenshot in Selenium and store it. Selenium WebDriver tool is used t
3 min read
How to Automate Click Using Selenium WebDriver?
Selenium is one of the most popular and powerful tools for automating web applications. Selenium is widely used for automating user interactions like filling out forms, clicking on a button, navigating to a web page, and many more. One of the most common tasks while working with Selenium is clicking
6 min read
How to do session handling in Selenium Webdriver using Java?
In Selenium WebDriver, managing browser sessions is crucial for ensuring that each test runs independently and without interference. A browser session in Selenium is identified by a unique session ID, which helps track and manage the session throughout the test. Proper session handling in Selenium W
4 min read
How to run Selenium Running Test on Chrome using WebDriver
Selenium is a popular open-source tool for automating web browser interactions. It works with different web browsers like Chrome, Firefox, and more, and different programming languages like Python or Java to write tests. What is a Selenium ChromeDriver?ChromeDriver is a separate executable that Sele
3 min read
File Upload using Selenium WebDriver and Java Robot Class
File uploads are a common task in web automation, and handling them effectively can be crucial for testing file-related functionalities. While Selenium WebDriver provides a straightforward way to interact with file upload elements, sometimes the built-in methods fall short. In such cases, combining
3 min read
How to Click on a Hyperlink Using Java Selenium WebDriver?
An open-source tool that is used to automate the browser is known as Selenium. Automation reduces human effort and makes the work comparatively easier. There are numerous circumstances in which the user wants to open a new page or perform a certain action with the click of the hyperlink. In this art
4 min read
How to refresh a webpage using java Selenium Webdriver?
Refreshing a webpage is often necessary when you want to ensure that your tests or interactions are working with the latest content or to reset the state of a web page during automation. Selenium WebDriver makes this task straightforward with various methods available in Java. This article will demo
3 min read
How to write e-mails in HTML and send it using Gmail ?
In this article, we are going to learn that how users can write e-mails in HTML format and send them using Gmail. However, Gmail doesn't offer an HTML editor still we can send HTML templates in an e-mail using some tools and methods. Many people need to send an e-mail with HTML templates to others.
3 min read
How to mute all sounds in chrome webdriver with selenium using Java?
In some automated testing situations, the website may play audio or video. This may interfere with the test operation. To solve this problem, turning off all sounds in Chrome during Selenium WebDriver testing ensures a quieter environment. It improves the efficiency and accuracy of test results. Thi
3 min read
How to capture Screen Shot in Selenium WebDriver?
Selenium is an open-source framework used mainly in web automation tasks. Its sole purpose is in testing web applications but it's not the only thing we can do with selenium. Selenium can also be used in other web-related tasks (such as web scraping, web element interaction, etc). Since Selenium is
4 min read