2
votes

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?

2

2 Answers

4
votes

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);
}
0
votes

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()) {
            ...
        }
    }
}