beginner
beginner

Reputation: 11

im Getting driver value=null in inherited class which is inherited from base class and also getting Null point exception(selenium)

I have also tried to write that method in base class but here also im getting driver value as null please see the attached photo I'm Getting driver value=null in inherited class which is inherited from base class and also getting Null point exception(selenium)

This is the code where I'm facing issue here two classes present one is base class and one is child class

    public class Base
    {
        public WebDriver driver;
        public Properties prop;

        public WebDriver initializeDriver() throws IOException {

            prop = new Properties();
            FileInputStream fis = new FileInputStream("C:\\Selenium\\projectsdemo\\src\\main\\java\\data.properties");

            prop.load(fis);
            String browserName = prop.getProperty("browser");
            System.out.println(browserName);

            if (browserName.equals("chrome")) {
                System.setProperty("webdriver.chrome.driver", "C:\\Selenium\\Browsersdrivers\\chromedriver.exe");
                driver = new ChromeDriver();
                //execute in chrome driver
                return driver;
            }

            //in below class where im inheriting here im getting value of driver=null;

        public class Screenshotusinglistener extends Base {
            public WebDriver driver;
            public void initialize() throws IOException {
                driver = initializeDriver();
                System.out.println("value of driver is" + driver);
            }

            public void Fail() throws Throwable {

                System.out.println("driver=" + driver);
                try {
                    File scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE); //Here getting null pointer may be driver value is null thats why
                    FileUtils.copyFile(scrFile, new File("d:\\Selenium\\Screenshots\\screenshot.png"));

                    } catch (Exception e) { // TODO: handle exception }
                        e.printStackTrace();
                }
            }
        }

the follow line errors:

File scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE); //Here getting null pointer may be driver value is null thats why

Upvotes: 1

Views: 801

Answers (2)

Rishi
Rishi

Reputation: 56

Edit: Add the following in Fail().

public void Fail() throws IOException {
        if (driver == null) {
                initializeDriver();
        }
        //Existing code.
}

Check if the browserName is equal to chrome. The case only in which the browserName is chrome is handled. That could be the issue too.

if (browserName.equals("chrome")) {
        driver = new ChromeDriver();
        //processing
        return driver;
}

Upvotes: 0

Partha
Partha

Reputation: 127

Return your driver in base class

public class Base {

public WebDriver driver;

public WebDriver initializedriver() {
    System.setProperty("webdriver.chrome.driver", "C:\\Drivers\\chromedriver.exe");
    driver = new ChromeDriver();
    return driver;
}

}

Upvotes: 1

Related Questions