我遇到过一种情况,即一个Selenium测试执行@AfterSuite
阻止,而另一个不执行testng.xml
阻止。
我正在使用testng.xml
运行测试。
最初我认为@AfterSuite
中可能存在一些配置问题;但我关闭了问题以测试案例级别,我的直觉说我在测试用例级别中缺少某些东西。
我的@AfterSuite
块实际上负责在测试执行结束后发送电子邮件,因此它是我项目中不同包的一部分;而不是在测试用例级别。
以下是执行package unitTest;
/*import statements*/
public class OpenBrowser extends ExtentReport {
@Test
public void launch() throws IOException, InterruptedException {
test = extent.createTest(Thread.currentThread().getStackTrace()[1].getMethodName());
Base.getBrowser();
System.out.println(ReadExcelData.readExcelData("Login.xlsx", 0, 0, 0));
Login.LoginField(Base.driver).sendKeys(ReadExcelData.readExcelData("Login.xlsx", 0, 1, 0));
Login.PasswordField(Base.driver).sendKeys(ReadExcelData.readExcelData("Login.xlsx", 0, 1, 1));
Login.SignIn(Base.driver).click();
}
@AfterTest
public void tearDown()
{
Base.closeBrowser();
}
}
:
@AfterSuite
并且不执行 package fundSearch;
/* Import Statements*/
public class FundNavigation extends ExtentReport {
@Test
public void verifyFundPage() {
test = extent.createTest(Thread.currentThread().getStackTrace()[1].getMethodName());
Base.getBrowser();
Funds.FundsLink(Base.driver).click();
Assert.assertEquals(Funds.SearchFundsField(Base.driver).isDisplayed(), true);
}
@AfterTest
public void tearDown()
{
Base.closeBrowser();
}
}
块的测试:
ExtentReport.java
package com....core;
/*Import Statements*/
public class ExtentReport {
public static ExtentHtmlReporter htmlReporter;
public static ExtentReports extent;
public static ExtentTest test;
public static String suiteName;
@BeforeSuite
public static void setUp(ITestContext ctx) {
// String currentDate=getDateTime();
suiteName = ctx.getCurrentXmlTest().getSuite().getName();
htmlReporter = new ExtentHtmlReporter(System.getProperty("user.dir") + "/Reports/" + suiteName + ".html");
extent = new ExtentReports();
extent.attachReporter(htmlReporter);
extent.setSystemInfo("OS", "Windows");
extent.setSystemInfo("Host Name", "CI");
extent.setSystemInfo("Environment", "QA");
extent.setSystemInfo("User Name", "QA_User");
htmlReporter.config().setChartVisibilityOnOpen(true);
htmlReporter.config().setDocumentTitle("AutomationTesting Report");
htmlReporter.config().setReportName("testReport");
htmlReporter.config().setTestViewChartLocation(ChartLocation.TOP);
htmlReporter.config().setTheme(Theme.STANDARD);
}
@AfterMethod
public void getResult(ITestResult result) throws IOException {
if (result.getStatus() == ITestResult.FAILURE) {
String screenShotPath = GetScreenShot.capture(Base.driver, "screenShotName", result);
test.log(Status.FAIL, MarkupHelper.createLabel(result.getName() + " Test case FAILED due to below issues:",
ExtentColor.RED));
test.fail(result.getThrowable());
test.fail("Snapshot below: " + test.addScreenCaptureFromPath(screenShotPath));
} else if (result.getStatus() == ITestResult.SUCCESS) {
test.log(Status.PASS, MarkupHelper.createLabel(result.getName() + " Test Case PASSED", ExtentColor.GREEN));
} else {
test.log(Status.SKIP,
MarkupHelper.createLabel(result.getName() + " Test Case SKIPPED", ExtentColor.ORANGE));
test.skip(result.getThrowable());
}
extent.flush();
}
@AfterSuite
public void tearDown() throws Exception {
System.out.println("In After Suite");
SendEmail.execute(SendEmail.path);
}
public static String getDateTime() {
// Create object of SimpleDateFormat class and decide the format
DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
// get current date time with Date()
Date date = new Date();
// Now format the date
String currentDate = dateFormat.format(date);
String newDate = currentDate.replace('/', '_');
String newCurrentDate = newDate.replace(':', '.');
return newCurrentDate;
}
}
位于以下位置:
/*Package Statement and Import Statements*/
public class SendEmail {
public static String path=System.getProperty("user.dir")+"/Reports/"+ExtentReport.suiteName+".html";
static String[] to={"xxx"};
static String[] cc={};
static String[] bcc={};
public static void execute(String reportFileName) throws Exception {
SendEmail.sendMail("xxx@gmail.com",
"xxx",
"smtp.gmail.com",
"465",
"true",
"true",
true,
"javax.net.ssl.SSLSocketFactory",
"false",
to,
cc,
bcc,
"Automation report",
System.getProperty("user.dir")+"/Reports/"+ExtentReport.suiteName+".html",
path,
reportFileName);
}
public static boolean sendMail(String userName,
String passWord,
String host,
String port,
String starttls,
String auth,
boolean debug,
String socketFactoryClass,
String fallback,
String[] to,
String[] cc,
String[] bcc,
String subject,
String text,
String attachmentPath,
String attachmentName){
//Object Instantiation of a properties file.
Properties props = new Properties();
props.put("mail.smtp.user", userName);
props.put("mail.smtp.host", host);
if(!"".equals(port)){
props.put("mail.smtp.port", port);
}
if(!"".equals(starttls)){
props.put("mail.smtp.starttls.enable",starttls);
props.put("mail.smtp.auth", auth);
}
if(debug){
props.put("mail.smtp.debug", "true");
}else{
props.put("mail.smtp.debug", "false");
}
if(!"".equals(port)){
props.put("mail.smtp.socketFactory.port", port);
}
if(!"".equals(socketFactoryClass)){
props.put("mail.smtp.socketFactory.class",socketFactoryClass);
}
if(!"".equals(fallback)){
props.put("mail.smtp.socketFactory.fallback", fallback);
}
try{
Session session = Session.getDefaultInstance(props, null);
session.setDebug(debug);
MimeMessage msg = new MimeMessage(session);
msg.setText(text);
msg.setSubject(subject);
Multipart multipart = new MimeMultipart();
MimeBodyPart messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(attachmentPath);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(attachmentName);
multipart.addBodyPart(messageBodyPart);
msg.setContent(multipart);
msg.setFrom(new InternetAddress(userName));
for(int i=0;i<to.length;i++){
msg.addRecipient(Message.RecipientType.TO, new
InternetAddress(to[i]));
}
for(int i=0;i<cc.length;i++){
msg.addRecipient(Message.RecipientType.CC, new
InternetAddress(cc[i]));
}
for(int i=0;i<bcc.length;i++){
msg.addRecipient(Message.RecipientType.BCC, new
InternetAddress(bcc[i]));
}
msg.saveChanges();
Transport transport = session.getTransport("smtp");
transport.connect(host, userName, passWord);
transport.sendMessage(msg, msg.getAllRecipients());
transport.close();
return true;
} catch (Exception mex){
mex.printStackTrace();
return false;
}
}
SendEmail.java如下:
testng.xml
}
我的<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="DefaultSuite" >
<!-- <listeners>
<listener class-name="org.uncommons.reportng.HTMLReporter"/>
<listener class-name="org.uncommons.reportng.JUnitXMLReporter"/>
</listeners>-->
<test name="Test" >
<classes>
<class name="unitTest.OpenBrowser" />
</classes>
</test>
</suite>
定义如下(第一次测试):
testng.xml
第二次测试的<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="DefaultSuite" >
<!-- <listeners>
<listener class-name="org.uncommons.reportng.HTMLReporter"/>
<listener class-name="org.uncommons.reportng.JUnitXMLReporter"/>
</listeners>-->
<test name="Test" >
<classes>
<class name="fundSearch.FundNavigation" />
</classes>
</test>
</suite>
如下所示。
@Aftersuite
当执行第一个测试而不是第二个测试运行时,是否有任何人请指出我做错了什么/差异,{{1}}正在运行?