I am working with ExtentReports and ItestListener for my testng-selenium-java project, My listener takes screenshot for the failes test case for ExtentReports but the problem is that I have multiple classes in my testng.XML and I run them in one go, one after the other doing different things and having own drivers.
In the failed case the code for Ilistener is -
public void onTestFailure(ITestResult iTestResult)
{
System.out.println("I am in onTestFailure method " +
getTestMethodName(iTestResult) + " failed");
//Get driver from BaseTest and assign to local webdriver variable.
Object testClass = iTestResult.getInstance();
WebDriver webDriver = ((BaseTest) testClass).getDriver();
//Take base64Screenshot screenshot.
String base64Screenshot = "data:image/png;base64,"+((TakesScreenshot)webDriver).
getScreenshotAs(OutputType.BASE64);
//Extentreports log and screenshot operations for failed tests.
ExtentTestManager.getTest().log(LogStatus.FAIL,"Test Failed",
ExtentTestManager.getTest().addBase64ScreenShot(base64Screenshot));
}
How to make sure that the driver of the failed test case's class is taken whenever a test case is failed, because in the above code only one class's driver is given all the time and not the current class's.