How do I pass the correct driver of a failed (java) selenium test to a testng ITestListener, when running multiple tests?

I have a TestListener class, which implements ITestListener, which is responsible for taking pictures and saving the html if a selenium test fails.

My selenium tests have a parent class BaseTest, which helps do common tasks, like initiating each driver.

The content of each important area:

TestListener

public class TestListener implements ITestListener {
    WebDriver driver=null;
    ITestContext context = null;
    String filePath = "artifacts/";

    @Override
    public void onTestFailure(ITestResult result) {
        ITestContext context = result.getTestContext();
        driver = (WebDriver) context.getAttribute("WebDriver");
        System.out.println("***** Error "+result.getName()+" test has failed *****");
        String methodName=result.getName().toString().trim();
        String currentTime = getCurrentTime();
        saveScreenShot(methodName, currentTime);
        savePageSource(methodName, currentTime);
        saveConsoleLog(methodName, currentTime);
    }
...

BaseTest (each test calls this separately)

    private void testngSetup(ITestContext context){
        context.setAttribute("WebDriver", driver);
        TestRunner runner = (TestRunner) context;
        runner.setOutputDirectory(artifactLocation);
    }

pom.xml

        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>3.2.5</version>
          <configuration>
            <suiteXmlFiles>
              <suiteXmlFile>testng.xml</suiteXmlFile>
            </suiteXmlFiles>
            <useSystemClassLoader>false</useSystemClassLoader>
          </configuration>
        </plugin>

testng.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="esm4hpc">
    <listeners>
        <listener class-name="selenium.listeners.TestListener"/>
    </listeners>
    <test name="sample-Test" verbose="2" parallel = "classes">
        <packages>
            <package name=".*"/>
        </packages>
    </test>
</suite>

I have found this thread (How to get the current class driver in ItestListener), which suggested to change getting the driver in ITestListener the following way: (WebDriver)result.getTestClass().getRealClass().getDeclaredField("driver").get(result.getInstance()), but it throw java.lang.NoSuchFieldException : driver

public class TestListener implements ITestListener {
    WebDriver driver=null;
    ITestContext context = null;
    String filePath = "artifacts/";

    @Override
    public void onTestFailure(ITestResult result) {
        ITestContext context = result.getTestContext();
        driver = (WebDriver)result.getTestClass().getRealClass().getDeclaredField("driver").get(result.getInstance());
        System.out.println("***** Error "+result.getName()+" test has failed *****");
        String methodName=result.getName().toString().trim();
        String currentTime = getCurrentTime();
        saveScreenShot(methodName, currentTime);
        savePageSource(methodName, currentTime);
        saveConsoleLog(methodName, currentTime);
    }
...

I have also tried to set the parallel tag in testng.xml to false, but did not seem to help.
I also tried to list each testing class separately, but it also gave back wrong pictures.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="esm4hpc">
    <listeners>
        <listener class-name="selenium.listeners.TestListener"/>
    </listeners>
    <test name="sample-Test" verbose="2" parallel = "false">
        <classes>
            <class name="selenium.esm4hpc.AuthenticationTest"/>
            <class name="selenium.esm4hpc.FileEditorTest"/>
            <class name="selenium.esm4hpc.FileManagerTest"/>
            <class name="selenium.esm4hpc.JobTest"/>
            <class name="selenium.esm4hpc.ProjectTest"/>
        </classes>
    </test>
</suite>

The only thing seemed to help is to move each test class into different test-cases:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="esm4hpc">
    <listeners>
        <listener class-name="selenium.listeners.TestListener"/>
    </listeners>
    <test name="AuthenticationTest" verbose="2" parallel = "false">
        <classes>
            <class name="selenium.esm4hpc.AuthenticationTest"/>
        </classes>
    </test>
    <test name="FileEditorTest" verbose="2" parallel = "false">
        <classes>
            <class name="selenium.esm4hpc.FileEditorTest"/>
        </classes>
    </test>
    <test name="FileManagerTest" verbose="2" parallel = "false">
        <classes>
            <class name="selenium.esm4hpc.FileManagerTest"/>
        </classes>
    </test>
    <test name="JobTest" verbose="2" parallel = "false">
        <classes>
            <class name="selenium.esm4hpc.JobTest"/>
        </classes>
    </test>
    <test name="ProjectTest" verbose="2" parallel = "false">
        <classes>
            <class name="selenium.esm4hpc.ProjectTest"/>
        </classes>
    </test>
</suite>

But this causes the result files to be generated separately, which makes it cumbersome to debug failed cases.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật