问题
Here I am using the listener for generating reports in HTML format but it is not printing the logs present in a test case.
Sample Test Cases
@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();
}
Method Present in BaseClass
public void reportLog(String message) {
message = BREAK_LINE + message;
logger.info("Message: " + message);
Reporter.log(message);
}
ExtentReport Listener
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 the screenshot.
回答1:
The Below Code Works for me for Extent Report version : v2.41.1, Try it!!!
Try Creating an Object of Extent Report and logger as the Below Code.
// Base Class Usage
public static ExtentReports report;
public static ExtentTest logger;
report = new ExtentReports(path);
report.loadConfig(new File("//home//.....//extent-config.xml"));
logger = report.startTest(this.getClass().getSimpleName()).assignCategory("Happy Path"));
// Test Case Usage: Using it at Every Step in Each Test Case
logger.log(LogStatus.INFO,"String Message to Log for Each Step in Test Case");
// @AfterMethod
@AfterMethod(alwaysRun=true)
public void TearDown_AM(ITestResult result) throws IOException
{
System.out.println("@After Method");
try
{
if(result.getStatus()==ITestResult.FAILURE)
{
String res = captureScreenshot(Driver, result.getName());
String image= logger.addScreenCapture(res);
System.out.println(image);
String TestCaseName = this.getClass().getSimpleName() + " Test Case Failure and Title/Boolean Value Failed";
logger.log(LogStatus.FAIL, TestCaseName + logger.addScreenCapture(res));
// logger.log(LogStatus.FAIL, image, this.getClass().getSimpleName() + " Test Case Failure and Title/Boolean Value Failed");
}
else if(result.getStatus()==ITestResult.SUCCESS)
{
logger.log(LogStatus.PASS, this.getClass().getSimpleName() + " Test Case Success and Title Verified");
}
else if(result.getStatus()==ITestResult.SKIP)
{
logger.log(LogStatus.SKIP, this.getClass().getSimpleName() + " Test Case Skipped");
}
report.endTest(logger);
report.flush();
}
catch(Throwable t)
{
logger.log(LogStatus.ERROR,t.fillInStackTrace());
}
}
回答2:
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);
}
Sample 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();
}
- ExtentReport view
回答3:
I know this question is little old but might help someone. Use this code for buildTestNodes method to print TestNG logs - Reporter.log().
public void buildTestNodes(IResultMap tests, LogStatus status) {
//ExtentTest test = null;
if (tests.size() > 0) {
List<ITestResult> resultList = new LinkedList<ITestResult>(tests.getAllResults());
class ResultComparator implements Comparator<ITestResult> {
public int compare(ITestResult r1, ITestResult r2) {
return getTime(r1.getStartMillis()).compareTo(getTime(r2.getStartMillis()));
}
}
Collections.sort(resultList , new ResultComparator ());
for (ITestResult result : resultList) {
test = extent.startTest(result.getMethod().getMethodName());
test.getTest().setDescription(result.getMethod().getDescription());
test.getTest().setStartedTime(getTime(result.getStartMillis()));
test.getTest().setEndedTime(getTime(result.getEndMillis()));
for(String message:Reporter.getOutput(result)){ **//This code picks the log from Reporter object.**
test.log(LogStatus.INFO, message);
}
for (String group : result.getMethod().getGroups())
test.assignCategory(group);
String message = "Test " + status.toString().toLowerCase() + "ed";
if (result.getThrowable() != null)
message = result.getThrowable().getClass() +": "+ result.getThrowable().getMessage();
test.log(status, message);
extent.endTest(test);
}
}
}
来源:https://stackoverflow.com/questions/44542760/how-to-print-logs-by-using-extentreports-listener-in-java