问题
I'm messing around with Selenium Java Extent Reports. Their new version is out as of Oct 12 but I'm not seeing the endTest method. They haven't released their full documentation for v3.0.0 yet. Most everything is about the same usage-wise but the endTest method no longer seems to be available.
Does anyone know how to end a test run so that multiple tests can be displayed in the same report file?
report = ExtentFactory.getInstance(date, time);
test = report.createTest("mytest");
test.log(Status.INFO, "test started");
// do some other stuff
report.endTest(test); <-- this is no longer an option.
Anyone know what the new way to end a test is?
All I can find is
report.close();
but that doesn't seem to let me put multiple tests into the same report.
回答1:
Version 3 is quite different - you now have the ability to decide which reporters you require. The below example uses both Html and ExtentX:
ExtentHtmlReporter htmlReporter = new ExtentHtmlReporter("filePath");
ExtentXReporter extentxReporter = new ExtentXReporter("host");
ExtentReports extent = new ExtentReports();
extent.attachReporter(htmlReporter, extentxReporter);
Individual tests no longer need to be ended, you only have to worry about logging events. The below will start and add 2 tests to the report:
extent.createTest("Test1").pass("pass");
extent.createTest("Test2").error("error");
Writing to the results file is the same as before:
extent.flush();
Depending upon your test-runner (I will show how to use it with TestNG), you now have to create tests and add information to them such as below (the below approach supports multi-threading):
public class ExtentTestNGReportBuilder {
private ThreadLocal<ExtentTest> parentTest;
private ThreadLocal<ExtentTest> test;
@BeforeClass
public synchronized void beforeClass() {
ExtentTest parent = ExtentTestManager.createTest(getClass().getName());
parentTest.set(parent);
}
@BeforeMethod
public synchronized void beforeMethod(Method method) {
ExtentTest child = parentTest.get().createNode(method.getName());
test.set(child);
}
@AfterMethod
public synchronized void afterMethod(ITestResult result) {
if (result.getStatus() == ITestResult.FAILURE)
test.get().fail(result.getThrowable());
else if (result.getStatus() == ITestResult.SKIP)
test.get().skip(result.getThrowable());
else
test.get().pass("Test passed");
ExtentManager.getExtent().flush();
}
}
The above is just to give you an idea, you can find the entire codebase here: https://github.com/anshooarora/extentreports-java/issues/652#issuecomment-254078018
来源:https://stackoverflow.com/questions/40220370/extent-report-report-endtesttest-method