要从不同的TestNG文件生成单个HTML电子邮件报表,你可以使用以下步骤:
以下是一个简单的示例,展示如何使用ExtentReports和JavaMail发送HTML报告:
import com.aventstack.extentreports.ExtentReports;
import com.aventstack.extentreports.reporter.ExtentHtmlReporter;
public class TestNGListener implements ITestListener {
private static ExtentReports extent = new ExtentReports();
@Override
public void onStart(ITestContext context) {
ExtentHtmlReporter htmlReporter = new ExtentHtmlReporter("test-output/ExtentReport.html");
extent.attachReporter(htmlReporter);
}
@Override
public void onFinish(ITestContext context) {
extent.flush();
}
}
import javax.mail.*;
import javax.mail.internet.*;
import java.util.Properties;
public class EmailSender {
public static void sendEmailWithAttachment(String to, String subject, String body, String attachmentPath) throws Exception {
Properties properties = new Properties();
properties.put("mail.smtp.host", "smtp.example.com");
properties.put("mail.smtp.port", "587");
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.starttls.enable", "true");
Session session = Session.getInstance(properties, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("username", "password");
}
});
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("from@example.com"));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
message.setSubject(subject);
MimeBodyPart textPart = new MimeBodyPart();
textPart.setText(body);
MimeBodyPart attachmentPart = new MimeBodyPart();
attachmentPart.attachFile(new File(attachmentPath));
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(textPart);
multipart.addBodyPart(attachmentPart);
message.setContent(multipart);
Transport.send(message);
}
}
通过以上步骤和示例代码,你可以实现从多个TestNG文件生成单个HTML电子邮件报表的功能。
没有搜到相关的沙龙
领取专属 10元无门槛券
手把手带您无忧上云