空指针异常在打开 Chrome 浏览器后发生(Selenium)

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

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&#39;m building a new Selenium framework in my company. 
As mentioned below, I&#39;m calling a method &quot;StartBrowser()&quot; in base class from browser class. My code is executing properly until &quot;StartBrowser()&quot; but after that, it&#39;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(&quot;user.dir&quot;)+&quot;\\src\\main\\java\\config\\config.properties&quot;);
		prop.load(fis);
		String browserName=prop.getProperty(&quot;browser&quot;);
		String LADSurl=prop.getProperty(&quot;LADSurl&quot;);
		String GADSurl=prop.getProperty(&quot;GADSurl&quot;);
		String chromeDriverPath=prop.getProperty(&quot;ChromeDriverPath&quot;);
		System.out.println(browserName);
		
		if (browserName.equalsIgnoreCase(&quot;chrome&quot;))
		{
			System.setProperty(&quot;webdriver.chrome.driver&quot;, chromeDriverPath);
			driver=new ChromeDriver();
		}
		else if(browserName.equalsIgnoreCase(&quot;firefox&quot;))
		{	
		}
		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(&quot;user.dir&quot;) + &quot;\\src\\main\\java\\config\\config.properties&quot;);
		prop.load(fis);
		String browserName = prop.getProperty(&quot;browser&quot;);
		String LADSurl = prop.getProperty(&quot;LADSurl&quot;);
		String GADSurl = prop.getProperty(&quot;GADSurl&quot;);
		String chromeDriverPath = prop.getProperty(&quot;ChromeDriverPath&quot;);
	}
	public static void OpenApplication(String environment) throws IOException {
		BrowserFactory browser=new BrowserFactory();
		browser.StartBrowser();
		if (environment.equalsIgnoreCase(&quot;LADS&quot;)) {
			driver.get(&quot;www.google.com&quot;); // Local ADS URL
		} else if (environment.equalsIgnoreCase(&quot;GADS&quot;)) {
			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 &#226;œ&#189;.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;

huangapple
  • 本文由 发表于 2020年7月26日 13:38:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/63096494.html
匿名

发表评论

匿名网友

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

确定