0

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 :

@Listeners(CustomListener.class)
public class AjoutPanier {
    public WebDriver driver;
    
    @BeforeTest
    public void LaunchWebsite() {
        // Launch Chrome
        System.setProperty("webdriver.chrome.driver", "C:\\chromedriver.exe");
        driver = new ChromeDriver();
        // Acces Website
        driver.get("https://pileouface.07zr.lu/fr/home/");
        driver.manage().window().maximize();
        Reporter.log(FormatMessageSucces + " Chrome is Opened</font>");
    }.....}

Class B :

public class CustomListener extends AjoutPanier implements ITestListener{
    
    @Override
    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 {
                System.out.println("try");
                FileUtils.copyFile(file, ScreenshotName);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                System.out.println("catch");
                e.printStackTrace();
            }
            Reporter.log("</br><img src='"+ ScreenshotName +"'/>");
            System.out.println("*******Screenshot captured********");
    }
}
4
  • 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
    Nov 22 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
    Nov 22 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.
    Nov 22 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
    Nov 23 at 7:14
0

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

public class SeleniumTestData {
    public static WebDriver webDriver;
}

and

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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