I was about to go for the solution provided by Robbie, but wanted to avoid binding my base class. Since I used Guice to inject my WebDriver provider, I decided to pass the instance through the TestNG attribute, connecting it once in the installation test class as follows:
public class Setup { @Inject WebDriver driver; @BeforeSuite public void onStart(ITestContext testContext) { testContext.setAttribute("WebDriver", this.driver); } }
Then in my listener I just pulled it out:
@Override public void onTestFailure(ITestResult result) { Object webDriverAttribute = result.getTestContext().getAttribute("WebDriver");
I was hoped for the best way that did not require casting, but had not yet found it.
Jackc source share