java 如何全局初始化 Web 驱动程序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30747739/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I initialize web driver globally?
提问by Helping Hands
I am working on automation using selenium webdriver , java. Getting nullpointerexceptionand it says driver is null.
我正在使用 selenium webdriver 和 java 进行自动化。获取nullpointerexception并表示驱动程序为空。
My code structure is given below :
我的代码结构如下:
Package Utility
包实用程序
- Base Class
- Login Class
- App_constant Class
- 基类
- 登录类
- App_constant 类
Package Add user
套餐添加用户
- Add user Class
- 添加用户类
Utility package code :
实用程序包代码:
package Utility;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class Base {
public static WebDriver driver = null;
//CALL WEB BROWSER AND OPEN WEBSITE
public static void openURL()
{
try{
System.setProperty("webdriver.chrome.driver", "D:\chromedriver.exe");
driver = new ChromeDriver();
driver.get(Constant_value_utility.URL);
}catch(Exception E)
{
E.printStackTrace();
}
}
}
package Utility;
public class Constant_value_utility {
//OPEN URL
public static final String URL = "Site URL";
//LOGIN FIELDS
public static final String loginbox = "UserName";
public static final String passbox = "Password";
//LOGIN DATA
public static final String username = "test";
public static final String password = "test";
public static final String loginbt = "btnsubmit";
}
package Utility;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
public class Login_Page {
public static WebDriver driver;
public static void login()
{
Base.openURL();
driver.manage().window().maximize();
driver.findElement(By.id(Constant_value_utility.loginbox)).sendKeys(Constant_value_utility.username);
driver.findElement(By.id(Constant_value_utility.passbox)).sendKeys(Constant_value_utility.password);
driver.findElement(By.id(Constant_value_utility.loginbt)).click();
}
}
}
Add user Package code
添加用户包代码
package Adduser;
import Utility.Base;
import Utility.Login_Page;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class Add_User {
public static void main(String[] args){
{
//LOGIN TO SITE
Base.openURL();
Login_Page.login();
}}}
Now My question is I have already created public static methodopenurl() in base class and webdriver is initialized there. But When I call same method in other class of same package and other packages , Why it is giving me nullpointerexception for webdriver?
现在我的问题是我已经在基类中创建了公共静态方法openurl() 并且在那里初始化了 webdriver。但是当我在同一个包和其他包的其他类中调用相同的方法时,为什么它给我 webdriver 的 nullpointerexception?
Is that necessary to write code to initialize webdriver and call browser in every class. How can I initialize web driver globally so I declare it once and can call any where in my project.
是否有必要编写代码来初始化 webdriver 并在每个类中调用浏览器。如何全局初始化 Web 驱动程序,以便我声明一次并可以调用项目中的任何位置。
回答by Manuel Jain
your webDriver is declared in the other class too, so
你的 webDriver 也在另一个类中声明,所以
Base.openURL();
driver.manage().window().maximize();
driver is not initialized here.
此处未初始化驱动程序。
try to rewrite your Base.openUrl()
method in order to return the webDriver
尝试重写您的Base.openUrl()
方法以返回 webDriver
edit: your class fields are visible to other classes, thats true. but in order to get the one from the correct class you should try something like Base.driver
because Base.driver != Login_Page.driver
编辑:您的班级字段对其他班级可见,这是真的。但为了从正确的班级中获得一个,你应该尝试类似的事情,Base.driver
因为Base.driver != Login_Page.driver
edit2: here is one example of how a working class could look like
编辑 2:这是一个工人阶级可能是什么样子的例子
package Utility;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
public class Login_Page {
//public static WebDriver driver;
public static void login()
{
Base.openURL();
//note the change from driver to Base.driver
Base.driver.manage().window().maximize();
Base.driver.findElement(By.id(Constant_value_utility.loginbox)).sendKeys(Constant_value_utility.username);
Base.driver.findElement(By.id(Constant_value_utility.passbox)).sendKeys(Constant_value_utility.password);
Base.driver.findElement(By.id(Constant_value_utility.loginbt)).click();
}
}