问题描述
我想在测试报告中报告测试日志。我在ItestListner和Test类中也都调用了scopeReportManager实用程序。当在测试类中调用范围报告对象时,不执行测试,仅打开浏览器。但是,当仅在Itestlistner中调用时,将执行测试。但是testName仅在报告中打印
有人可以帮忙吗
TestClass
package com.selenium.tests;
import static org.testng.Assert.assertEquals;
import java.io.IOException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.Select;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
import com.aventstack.extentreports.ExtentReports;
import com.aventstack.extentreports.ExtentTest;
import com.aventstack.extentreports.Status;
import com.selenium.pageObjects.DashBoard;
import com.selenium.pageObjects.RecentActivity;
import com.selenium.pageObjects.Setting;
import com.selenium.pageObjects.SignIn;
import com.selenium.resources.ExtentReportManager;
import com.selenium.resources.base;
import junit.framework.Assert;
public class IcarxTests extends base {
public WebDriver driver;
SignIn signIn;
String getTitle;
DashBoard dashboard;
RecentActivity recentActivity;
ExtentReports extent = ExtentReportManager.getExtentReport();
public ExtentTest test;
@BeforeTest
public void intializeDriver() throws IOException {
driver = browserIntilization();
test.info("browser is launched sucessfully");
driver.get(prop.getProperty("Application_url"));
test.info(prop.getProperty("Application_url") + "is launched successfully");
}
@Test(priority = 1)
@Parameters({ "Market","Language" })
public void setting(String marketStr,String languageStr) throws IOException,InterruptedException {
System.out.println("1 test");
Setting setting = new Setting(driver);
setting.selectMarket(marketStr);
test.info(marketStr + "is selected");
setting.selectLanguage(languageStr);
test.info(languageStr + "is selected");
Thread.sleep(5000);
signIn = setting.navigateSignIn();
test.info("Navigated to signIn page sucessfully");
getTitle = signIn.getPageTitle();
Assert.assertEquals("ICAR-X",getTitle);
test.log(Status.PASS,"Sigin page tile" +getTitle+" is verified");
}
@Test(priority = 2)
@Parameters({ "dealerID","userID","password" })
public void login_Success(String dealerStr,String userIDStr,String passwordStr) throws InterruptedException {
System.out.println("2 test");
signIn.enterDealerID(dealerStr);
signIn.enterUserID(userIDStr);
signIn.enterPassword(passwordStr);
dashboard = signIn.naviagateDashBoard();
Thread.sleep(5000);
}
ExtentReport实用程序
package com.selenium.resources;
import com.aventstack.extentreports.ExtentReports;
import com.aventstack.extentreports.ExtentTest;
import com.aventstack.extentreports.reporter.ExtentSparkReporter;
import com.aventstack.extentreports.reporter.configuration.Theme;
public class ExtentReportManager {
public static ExtentSparkReporter reporter;
public static ExtentReports extent;
public static ExtentTest test;
public static ExtentReports getExtentReport() {
String filePath = System.getProperty("user.dir")+"\\reports\\extentReport.html";
ExtentSparkReporter reporter = new ExtentSparkReporter(filePath);
reporter.config().setDocumentTitle("Automation Test Report");
reporter.config().setReportName("ICAR Test Automation Report");
reporter.config().setTheme(Theme.STANDARD);
ExtentReports extent = new ExtentReports();
extent.attachReporter(reporter);
return extent;
}
}
**ITestListenr code**
package com.selenium.listeners;
import java.io.IOException;
import org.openqa.selenium.WebDriver;
import org.testng.ITestContext;
import org.testng.ITestListener;
import org.testng.ITestResult;
import com.aventstack.extentreports.ExtentReports;
import com.aventstack.extentreports.ExtentTest;
import com.aventstack.extentreports.Status;
import com.selenium.resources.ExtentReportManager;
import com.selenium.resources.base;
public class Listeners extends base implements ITestListener {
ExtentReports extent = ExtentReportManager.getExtentReport();
public ExtentTest test;
@Override
public void onFinish(ITestContext arg0) {
extent.flush();
}
@Override
public void onStart(ITestContext arg0) {
// Todo Auto-generated method stub
}
@Override
public void onTestFailedButWithinSuccesspercentage(ITestResult arg0) {
// Todo Auto-generated method stub
}
@Override
public void onTestFailure(ITestResult result) {
test.fail(result.getThrowable());
String testMethod = result.getmethod().getmethodName();
WebDriver driver = null;
try {
driver =(WebDriver)result.getTestClass().getRealClass().getDeclaredField("driver").get(result.getInstance());
} catch(Exception e)
{
}
try {
String destinationPath = getScreenShotPath(testMethod,driver);
test.addScreenCaptureFromPath(destinationPath,testMethod);
} catch (IOException e) {
// Todo Auto-generated catch block
e.printstacktrace();
}
test.log(Status.FAIL,testMethod + "Test is Failed");
}
@Override
public void onTestSkipped(ITestResult result) {
String testMethod = result.getmethod().getmethodName();
test.log(Status.SKIP,testMethod);
}
@Override
public void onTestStart(ITestResult result) {
String testMethod = result.getmethod().getmethodName();
test = extent.createTest(result.getmethod().getmethodName());
test.log(Status.PASS,testMethod + "Test is Started");
}
@Override
public void onTestSuccess(ITestResult result) {
String testMethod = result.getmethod().getmethodName();
test.log(Status.PASS,testMethod + "Test is Passed");
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)