Reputation: 2224
As part of Selenium Automation Framework, I need to write a method to generate custom TestNG report. I know this can be achieved by overriding
public void generateReport(List<XmlSuite> xmlSuites, List<ISuite> suites, String outputDirectory)
method in IReporter
interface. But the problem is that my test methods calculate some values and I have to pass these values to the testNG report. How can I print values from test method in testNG report?
Upvotes: 2
Views: 2072
Reputation: 14746
An ITestResult
object ( this object can be accessed from within a @Test
method by invoking Reporter.getCurrentTestResult()
) basically has a setAttribute
method which takes in a String key and whose value is an Object
object.
So you can simply employ something like below within your @Test
method to save values computed by your test into its corresponding ITestResult
object and then retrieve it from within your IReporter
implementation.
@Test
public void myTestMethod() {
Map<String, Object> computedItems = new HashMap<>();
//Lets assume that the computedItems is what we need to save for retrieval from our reports.
ITestResult testResult = Reporter.getCurrentTestResult();
testResult.setAttribute("key", computedItems);
}
Upvotes: 4
Reputation: 2599
All test data is stored in ITestResult:
for (ISuite suite : suites) {
...
for (ISuiteResult result : suite.getResults().values())
...
IResultMap iFailed = result.getTestContext().getFailedTests();
for(ITestResult itr: iFailed.getAllResults()) {
...
}
}
}
Upvotes: 0