我已经从testng.xml创建了一个测试套件,并希望使用emailable-report.xml自动触发电子邮件。我已经编写了触发电子邮件的代码,在运行该代码时,我可以发送电子邮件。但是,在运行将该电子邮件代码放入@AfterSuite的套件后,电子邮件不会被触发。
有人可以帮我解决这个问题,我需要做些什么来使这件事有效?
发送电子邮件的代码:
package ServiceUtils;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.AfterTest;
import org.testng.annotations.Test;
public class EMailClass{
public static Properties Config = new Properties();
@AfterSuite
public static void SendEmail() throws IOException
{
String abspath = new File("").getAbsolutePath();
String ConfigFileLocation = abspath + "/src/main/java/testProperties/Config.properties";
FileInputStream LoadFile = new FileInputStream(ConfigFileLocation);
Config.load(LoadFile);
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");
Session session = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(Config.getProperty("SenderEmailAddress"), Config.getProperty("SenderEmailPassword"));
}
});
try {
Message message = new MimeMessage(session);
// message.setFrom(new InternetAddress("test@gmail.com"));
message.setRecipients(Message.RecipientType.TO,InternetAddress.parse(Config.getProperty("ReceiverEmailAddress")));
message.setSubject(Config.getProperty("EmailSubject"));
BodyPart messageBodyPart1 = new MimeBodyPart();
messageBodyPart1.setText(Config.getProperty("EmailBody"));
MimeBodyPart messageBodyPart2 = new MimeBodyPart();
String filename = Config.getProperty("EmailFiletoAttach1");
DataSource source = new FileDataSource(filename);
messageBodyPart2.setDataHandler(new DataHandler(source));
messageBodyPart2.setFileName(filename);
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart2);
multipart.addBodyPart(messageBodyPart1);
message.setContent(multipart);
Transport.send(message);
System.out.println("=====Email Sent=====");
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
}
示例Testng.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<suite name="Regression Suite 1" verbose="1" >
<test name="Regression suite 1" >
<classes>
<class name="com.automation.cc"/>
</classes>
</test>
</suite>
答案 0 :(得分:0)
你没有在套件中包含camelCase
,然后TestNG无法知道它。试试:
EMailClass
或者你可以这样做:
<?xml version="1.0" encoding="UTF-8"?>
<suite name="Regression Suite 1" verbose="1" >
<test name="Regression suite 1" >
<classes>
<class name="com.automation.cc"/>
<class name="ServiceUtils.EMailClass"/>
</classes>
</test>
</suite>