英文:
Null pointer exception after opening chrome browser (Selenium)
问题
希望大家都过得很好。
请问你能帮我解决这个空指针问题吗?我正在公司建立一个新的Selenium框架。
如下所示,我从浏览器类中调用基类中的方法"StartBrowser()"。我的代码在执行"StartBrowser()"之前都正常运行,但在那之后就抛出了空指针异常。
非常感谢您提前的帮助。
{
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;
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
}
}
控制台错误:
Jul 26, 2020 1:49:05 PM cucumber.api.cli.Main run
WARNING: You are using deprecated Main class. Please use io.cucumber.core.cli.Main
Scenario: Create Student # src/test/resources/Features/CreateUsers.feature:3
chrome
Starting ChromeDriver 84.0.4147.30 (48b3e868b4cc0aa7e8149519690b6f6949e110a8-refs/branch-heads/4147@{#310}) on port 47410
Only local connections are allowed.
Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping ChromeDriver safe.
ChromeDriver was started successfully.
Jul 26, 2020 1:49:09 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C
Given User is on New User page # stepDefinitions.CreateUserSteps.user_is_on_new_user_page()
java.lang.NullPointerException
at utils.Base.OpenApplication(Base.java:39)
at stepDefinitions.CreateUserSteps.user_is_on_new_user_page(CreateUserSteps.java:38)
at ...
<details>
<summary>英文:</summary>
I hope you all are doing fine.
Could you please help me in fixing this null pointer issue? I'm building a new Selenium framework in my company.
As mentioned below, I'm calling a method "StartBrowser()" in base class from browser class. My code is executing properly until "StartBrowser()" but after that, it's throwing null pointer exception.
Thank you so much in advance.
{
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
}
}
Console error:
Jul 26, 2020 1:49:05 PM cucumber.api.cli.Main run
WARNING: You are using deprecated Main class. Please use io.cucumber.core.cli.Main
Scenario: Create Student # src/test/resources/Features/CreateUsers.feature:3
chrome
Starting ChromeDriver 84.0.4147.30 (48b3e868b4cc0aa7e8149519690b6f6949e110a8-refs/branch-heads/4147@{#310}) on port 47410
Only local connections are allowed.
Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping ChromeDriver safe.
ChromeDriver was started successfully.
Jul 26, 2020 1:49:09 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C
Given User is on New User page # stepDefinitions.CreateUserSteps.user_is_on_new_user_page()
java.lang.NullPointerException
at utils.Base.OpenApplication(Base.java:39)
at stepDefinitions.CreateUserSteps.user_is_on_new_user_page(CreateUserSteps.java:38)
at ✽.User is on New User page(file:///C:/Users/rgorilla/eclipse-workspace/ESA/src/test/resources/Features/CreateUsers.feature:4)
When User provide student information click Save User # stepDefinitions.CreateUserSteps.user_provide_student_information_click_save_user()
Then Validate user is created successfully # stepDefinitions.CreateUserSteps.validate_user_is_created_successfully()
</details>
# 答案1
**得分**: 0
在你的基类中,驱动程序实例为空,因为你没有对其进行赋值。
在你的`openApplication()`方法中添加以下内容:
```csharp
driver = browser.StartBrowser();
英文:
In your base class, driver instance is null because you are not assigning it.
add following in your openApplication() method
driver = browser.StartBrowser();
答案2
得分: 0
Null point exception因为driver实例而发生。在您的自动化框架中有更多的页面,因此每个页面都应该由相同的driver实例处理,否则您将获得null point exception。
请将您的代码从public WebDriver driver;
更改为static WebDriver driver;
。
英文:
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;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论