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

huangapple go评论65阅读模式
英文:

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]1 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"

"the follow line errors: > File scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE); //Here getting null pointer may be driver value is null thats why"

英文:

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

答案1

得分: 1

public class Base {

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

}

英文:

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;
}
}

答案2

得分: 0

Fail()中添加以下内容。

public void Fail() throws IOException {
        if (driver == null) {
                initializeDriver();
        }
        //现有代码。
}

检查browserName是否等于chrome。仅处理browserNamechrome的情况。这也可能是问题所在。

if (browserName.equals("chrome")) {
        driver = new ChromeDriver();
        //处理
        return driver;
}
英文:

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;
}

huangapple
  • 本文由 发表于 2020年8月5日 20:47:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/63265565.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定