In the following code, I am trying to get my driver from the class A to class B. Class B can't have a constructor and I tried making a base class, but either Class A gets driver null or Class B gets driver null.

Can someone point out what I am doing wrong?

Class A :

public class AjoutPanier {
    public WebDriver driver;
    public void LaunchWebsite() {
        // Launch Chrome
        System.setProperty("webdriver.chrome.driver", "C:\\chromedriver.exe");
        driver = new ChromeDriver();
        // Acces Website
        Reporter.log(FormatMessageSucces + " Chrome is Opened</font>");

Class B :

public class CustomListener extends AjoutPanier implements ITestListener{
    public void onTestFailure(ITestResult result) {
        // TODO Auto-generated method stub
        System.out.println("TestFailure acces");
            File file = ((TakesScreenshot)b.driver).getScreenshotAs(OutputType.FILE);
            File ScreenshotName = new File(".//ScreenShots//test.png");
            System.out.println("Files done");
            try {
                FileUtils.copyFile(file, ScreenshotName);
            } catch (IOException e) {
                // TODO Auto-generated catch block
            Reporter.log("</br><img src='"+ ScreenshotName +"'/>");
            System.out.println("*******Screenshot captured********");
  • Welcome to the community. Why can't class B have a constructor? Having a constructor is a basic construct when using classes/OOP.
    – Lee Jensen
    Commented Nov 22, 2021 at 16:12
  • @LeeJensen i tried adding it but since i am trying to override the onTestFailure of ItestListener it doesnt let me add a constructor with webdriver as the value it takes
    – Yan
    Commented Nov 22, 2021 at 16:13
  • 1
    Since your driver is not static you should think about objects. Not classes. Think about how the instance of your custom listener is created. This will give you the right answer.
    – Alexey R.
    Commented Nov 22, 2021 at 20:05
  • @AlexeyR. my custome listener is called if there is an exception so when i add a softassert and that softassert is false it should return false so it will return an exception and thus the onTestFailure will be called and since it extends to Ajouterpanier it should take the driver but it takes it as null i tried a lot of solutions creating a base methode still returns null every solution i found tried it but didnt work
    – Yan
    Commented Nov 23, 2021 at 7:14

1 Answer 1


In this case, the driver should be static. Here is an example:

public class SeleniumTestData {
    public static WebDriver webDriver;


public class commonSeleniumSteps {
    public void beforeScenario() {
        SeleniumTestData.webDriver = new ChromeDriver();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.