如何使用 Selenium WebDriver 发送电子邮件报告?
我们可以使用 Selenium WebDriver 发送电子邮件报告。电子邮件报告在自动化框架中是一个重要的功能。在回归测试套件组合执行完成后,必须发送一封电子邮件,以便全面了解测试结果。
通过电子邮件发送报告的方法如下:
使用 Java 库 - Apache Commons,可在以下链接找到:https://commons.apache.org/proper/commons-email/。
使用 Java mail JAR。详细信息可在以下链接找到:https://javaee.github.io/javamail/
配置 Java mail JAR 的步骤如下:
步骤 1:访问以下链接:https://mvnrepository.com/artifact/javax.mail/javax.mail-api/1.6.2,并通过点击突出显示的链接将 JAR 添加到项目中。

如果我们有一个 Maven 项目,请在 pom.xml 文件中添加依赖项,如下面的图片中突出显示的那样。

步骤 2:我们应该在单独的文件中生成报告。
示例
代码实现
import javax.mail.BodyPart;
import javax.mail.Session; import javax.mail.Transport; import java.util.Properties; import javax.activation.DataHandler; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.Multipart; import javax.mail.PasswordAuthentication; import javax.activation.DataSource; import javax.activation.FileDataSource;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
public class EmailReport {
public static void main(String[] args) {
// instance of Properties class
Properties p = new Properties();
// configure host server
p.put("mail.smtp.host", "smtp.yahoo.com");
// configure socket port p.put("mail.smtp.socketFactory.port", "529");
// configure socket factory
p.put ("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
// configure true authentication p.put("mail.smtp.auth", "true");
// configure smtp port
p.put("mail.smtp.port", "465");
// authentication with Session class
Session s= Session.getDefaultInstance(p, new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("mail id", "password");
}
});
try {
// instance of MimeMessage
Message m = new MimeMessage(s);
// address of Form
m.setFrom(new ("test12@yahoo.com"));
//address of recipient m.setRecipients(Message.RecipientType.TO,InternetAddress.
parse("test13@yahoo.com"));
// email subject text
m.setSubject("Email Report");
// configure multi-media email
BodyPart b = new MimeBodyPart();
// email body text
b.setText("Overall Selenium Test Report");
// configure multi-media email for another text BodyPart b1 = new MimeBodyPart();
// name of file to be attached to email
String s = "C:\TestResults.xlsx";
// pass filename
DataSource sr= new FileDataSource(s);
// set the handler
b2.setDataHandler(new DataHandler(sr));
// configure file
b2.setFileName(s);
// instance of class MimeMultipart
Multipart mm = new MimeMultipart();
// adding body texts
mm.addBodyPart(b1);
mm.addBodyPart(b);
// add email content
m.setContent(mm);
// email sending
Transport.send(m);
System.out.println("MAIL TRIGGERED");
} catch (MessagingException ex) {
throw new RuntimeException(ex);
}
}
}
广告
数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP