英文:
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
。仅处理browserName
为chrome
的情况。这也可能是问题所在。
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;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论