79110265

Date: 2024-10-21 13:33:15
Score: 5
Natty:
Report link

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.

Reasons:
  • Blacklisted phrase (1): stackoverflow
  • RegEx Blacklisted phrase (2.5): Do you have any
  • Long answer (-1):
  • Has code block (-0.5):
  • Contains question mark (0.5):
  • User mentioned (1): @Suhail
  • Starts with a question (0.5): When I use
  • Low reputation (1):
Posted by: Panek Adamek