本文介绍了如何在Java中使用ExtentReports侦听器打印日志?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在这里,我使用侦听器生成HTML格式的报告,但它没有打印出测试用例中存在的日志.
Here I am using the listener for generating reports in HTML format but it is not printing the logs present in a test case.
示例测试用例
@Test
public void testRedirectAllControlScreen() throws Exception {
reportLog("login using a valid IsoMetrix username and password.");
HomePage homePage = loginPage.login("username", "password");
reportLog("Go to All Control page");
AllControlPage allControlPage = homePage.navigateToControlPage();
reportLog("Verify All Control page");
allControlPage.verifyAllControlPage();
}
BaseClass中存在的方法
public void reportLog(String message) {
message = BREAK_LINE + message;
logger.info("Message: " + message);
Reporter.log(message);
}
ExtentReport侦听器
public class ExtentReporterNG implements IReporter {
private ExtentReports extent;
public void generateReport(List<XmlSuite> xmlSuites, List<ISuite> suites, String outputDirectory) {
extent = new ExtentReports(outputDirectory + File.separator + "ExtentReport.html", true);
for (ISuite suite : suites) {
Map<String, ISuiteResult> result = suite.getResults();
for (ISuiteResult r : result.values()) {
ITestContext context = r.getTestContext();
buildTestNodes(context.getPassedTests(), LogStatus.PASS);
buildTestNodes(context.getFailedTests(), LogStatus.FAIL);
buildTestNodes(context.getSkippedTests(), LogStatus.SKIP);
}
}
extent.flush();
extent.close();
}
private void buildTestNodes(IResultMap tests, LogStatus status) {
ExtentTest test;
if (tests.size() > 0) {
for (ITestResult result : tests.getAllResults()) {
test = extent.startTest(result.getMethod().getMethodName());
test.assignAuthor("360Log");
test.setStartedTime(getTime(result.getStartMillis()));
test.setEndedTime(getTime(result.getEndMillis()));
for (String group : result.getMethod().getGroups())
test.assignCategory(group);
int s = result.getStatus();
if (result.getStatus() == 1) {
test.log(status, "Test " + status.toString().toLowerCase() + "ed");
} else {
String screen = BaseTest.screen;
test.log(status, "Test " + status.toString().toLowerCase() + "ed " + test.addScreenCapture(screen));
}
extent.endTest(test);
}
}
}
}
PFA屏幕截图.
PFA the screenshot.
推荐答案
在不使用侦听器的情况下,我也可以实现相同的目的.我按照这样的建议在 Baseclass.java 中实现了扩展测试和扩展报告:
Without using listener I am able to achieve the same thing.I implemented the extent test and extent report in Baseclass.java as per people suggestion like this:
public static ExtentTest test;
public static ExtentReports extent;
@BeforeSuite
public void before() {
extent = new ExtentReports("target\\surefire-reports\\ExtentReport.html", true);
}
@BeforeMethod
public void setUp(Method method) throws Exception {
test = extent.startTest(method.getClass().getSimpleName(),method.getClass().getEnclosingMethod().getName());
test.assignAuthor("Vaibhav");
//Rest code will be generic for browser initiliazion.
}
@AfterSuite
public void tearDownSuite() {
// reporter.endReport();
extent.flush();
extent.close();
}
//Method for adding logs passed from test cases
public void reportLog(String message) {
test.log(LogStatus.INFO, message);//For extentTest HTML report
logger.info("Message: " + message);
Reporter.log(message);
}
示例测试用例
@Test
public void testRedirectAllControlScreen() throws Exception {
reportLog("login using a valid IsoMetrix username and password.");
HomePage homePage = loginPage.login("username", "password");
reportLog("Go to All Control page");
AllControlPage allControlPage = homePage.navigateToControlPage();
reportLog("Verify All Control page");
allControlPage.verifyAllControlPage();
}
- ExtentReport视图
- ExtentReport view
这篇关于如何在Java中使用ExtentReports侦听器打印日志?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!