打开chrome浏览器后出现空指针异常(Selenium)



我希望你们都做得很好。

你能帮我解决这个空指针问题吗?我正在公司中构建一个新的Selenium框架。如下所述,我正在调用一个方法";开始浏览器(("在浏览器类的基类中。我的代码正在正确执行,直到";开始浏览器(("但在那之后,它将抛出空指针异常。

事先非常感谢。

{
public WebDriver driver;
public WebDriver StartBrowser() throws IOException
{
Properties prop=new Properties();
FileInputStream fis=new FileInputStream(System.getProperty("user.dir")+"\src\main\java\config\config.properties");
prop.load(fis);
String browserName=prop.getProperty("browser");
String LADSurl=prop.getProperty("LADSurl");
String GADSurl=prop.getProperty("GADSurl");
String chromeDriverPath=prop.getProperty("ChromeDriverPath");
System.out.println(browserName);

if (browserName.equalsIgnoreCase("chrome"))
{
System.setProperty("webdriver.chrome.driver", chromeDriverPath);
driver=new ChromeDriver();
}
else if(browserName.equalsIgnoreCase("firefox"))
{   
}
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.manage().window().maximize();
return driver;
}

}

public class Base {
public static WebDriver driver;
BrowserFactory browser;
public static Properties prop;
public static String LADSurl;
public static String GADSurl;

enter code here
public static void setupPropertiesFile() throws IOException {
Properties prop = new Properties();
FileInputStream fis = new FileInputStream(
System.getProperty("user.dir") + "\src\main\java\config\config.properties");
prop.load(fis);
String browserName = prop.getProperty("browser");
String LADSurl = prop.getProperty("LADSurl");
String GADSurl = prop.getProperty("GADSurl");
String chromeDriverPath = prop.getProperty("ChromeDriverPath");
}
public static void OpenApplication(String environment) throws IOException {
BrowserFactory browser=new BrowserFactory();
browser.StartBrowser();
if (environment.equalsIgnoreCase("LADS")) {
driver.get("www.google.com"); // Local ADS URL
} else if (environment.equalsIgnoreCase("GADS")) {
driver.get(GADSurl); // Global ADS URL
} 

}

控制台错误:2020年7月26日下午1:49:05警告:您正在使用不推荐使用的Main类。请使用io.cucumber.core.cli.Main

场景:创建学生#src/test/resources/Features/CreateUsers.feature:3铬在端口47410上启动ChromeDriver 84.0.4147.30(48b3e868b4cc0aa7e8149519690b6f6949e110a8参考/分支头/4147@{#310}(只允许本地连接。请参阅https://chromedriver.chromium.org/security-considerations以获取有关保持ChromeDriver安全的建议。ChromeDriver已成功启动。2020年7月26日下午1:49:09 org.openqa.selenium.remote.ProtocolHandshake createSession信息:检测到的方言:W3C给定用户位于"新用户"页面#stepDefinitions.CreateUserSteps.User_is_on_nnew_User_page((java.lang.NullPointerException在utils。Base.OpenApplication(Base.java:39(在步骤Definitions.CreateUserSteps.user_is_on_nnew_user_page(CreateUserSteps.java:38(在。用户在"新用户"页面上(file:///C:/Users/rgorilla/eclipse-workspace/ESA/src/test/resources/Features/CreateUsers.feature:4(

当用户提供学生信息时,单击保存用户#stepDefinitions.CreateUserSteps.User_provie_student_information_click_Save_User((然后验证用户成功创建#stepDefinitions.CreateUserSteps.Validate_user_is_created_successfully((

在基类中,驱动程序实例为null,因为您没有分配它。

在您的openApplication((方法中添加以下内容

driver = browser.StartBrowser();
Null point exception happening because of driver instance .In your automation framework have more pages so every pages should handled by same driver instance otherwise you will get null point exception 
Plese change your code to  **static WebDriver driver**; from public WebDriver driver;

最新更新