When I use:
public class TestListener implements ITestListener {
@Override
public void onTestFailure(ITestResult result) {
WebDriver driver = null;
try {
driver = (WebDriver) result.getTestClass()
.getRealClass()
.getDeclaredField("driver")
.get(result.getInstance());
if (driver != null) {
screenshotUtility = new ScreenshotUtility(driver);
String testName = result.getName();
screenshotUtility.captureScreenshot(testName);
String errorMessage = result.getThrowable().getMessage();
Allure.addAttachment("Error Message", errorMessage);
} catch (IllegalArgumentException | IllegalAccessException | NoSuchFieldException | SecurityException e) {
e.printStackTrace();
}
}
driver is null.
I described my other classes here: [https://stackoverflow.com/questions/79102969/allure-doesnt-capture-screenshots-in-listener].
Do you have any idea why it doesn't work? In BaseTest I have a variable set :
protected WebDriver driver;
I don't want to use the answer @Suhail Ahmed becouse,I wouldn't like to have a parameter (ITestContext context) in every test class.