I'm trying to categorize TestNG failures based on the type of exception/error causing the error. Is there some way to do this? I'm relatively new to TestNG, so would appreciate any help possible
Yes you can do this by following the below overall approach.
org.testng.IReporter
ITestResult
object which represents the results of a test method and then examine its exception via org.testng.ITestResult#getThrowable
and then include your logic to classify failures.Here's a draft implementation
public class SampleReporter implements IReporter {
@Override
public void generateReport(List<XmlSuite> xmlSuites, List<ISuite> suites, String outputDirectory) {
for (ISuite suite : suites) {
Map<String, ISuiteResult> suiteResults = suite.getResults();
for (ISuiteResult sr : suiteResults.values()) {
ITestContext tc = sr.getTestContext();
Set<ITestResult> failedResults = tc.getFailedTests().getAllResults();
for (ITestResult failedResult : failedResults) {
Throwable throwable = failedResult.getThrowable();
if (throwable instanceof WebDriverException) {
//classify this as a selenium exception
}
}
}
}
}
}
You can now decide to wire in this listener using one of the below options
@Listeners
annotation on your test class.<listeners>
tag in your suite xml fileYou can refer to this blog post of mine to learn more about TestNG listeners in general and all the above listed ways of listener injection.