使用selenium web驱动程序在页面对象页面上获取空指针异常

使用selenium web驱动程序在页面对象页面上获取空指针异常,selenium,nullpointerexception,Selenium,Nullpointerexception,我是Selenium web驱动程序的新手。 我已尝试创建Selenium web驱动程序框架。但由于此空指针异常,我被卡住了。 我创建了一个Testcase类、基类和一个Page对象类 TestCase Class://类名 package com.uiautomation.com.uiautomation.testscripts; import org.openqa.selenium.WebDriver; import org.testng.annotations.Test; import

我是Selenium web驱动程序的新手。 我已尝试创建Selenium web驱动程序框架。但由于此空指针异常,我被卡住了。 我创建了一个Testcase类、基类和一个Page对象类

TestCase Class://类名

package com.uiautomation.com.uiautomation.testscripts;
import org.openqa.selenium.WebDriver;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.AfterClass;
import com.uiautomation.base.LoginBase;
import com.uiautomation.setup.setBrowser;

public class LoginTest{
 WebDriver driver;
 setBrowser browser= new setBrowser();

  @BeforeClass
  public void beforeClass() {
      browser.setupwebdriver("FF");
  }

  @Test()
  public void loginPage() {
      LoginBase base = new LoginBase(driver);//create a new instance of Login base class
      base.loginToDummySite("Vishalbha", "abc123");//call base class method and pass parameters value
  }


  @AfterClass
  public void afterClass() {
  }

}
基类:

package com.uiautomation.base;
import org.openqa.selenium.WebDriver;
import com.uiautomation.pageobjects.LoginPO;

public class LoginBase {

     WebDriver driver;
    public LoginBase(WebDriver driver){
        this.driver= driver;
    }

    public void loginToDummySite(String UserName, String Password){
        System.out.println(UserName);
        LoginPO loginPO= new LoginPO(driver);// create instance of loginPO class
        loginPO.enterUserName(UserName);
        loginPO.enterPassword(Password);
        loginPO.loginButtonClick();
    }
}
页面对象类:

package com.uiautomation.pageobjects;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;

public class LoginPO {
    WebDriver driver;
    public LoginPO(WebDriver driver){
        PageFactory.initElements(driver, this);
        this.driver= driver;
    }

    @FindBy(id = "username")
     private static WebElement userNameTextbox;

    @FindBy(id="password")
    private WebElement passwordTextbox;

    @FindBy(id="login")
    private WebElement loginButton;


    public void enterUserName(String UserName){
        userNameTextbox.clear();
        userNameTextbox.sendKeys(UserName);
    }
    public void enterPassword(String Password){
        passwordTextbox.clear();
        passwordTextbox.sendKeys(Password);
    }   
    public void loginButtonClick(){
        loginButton.click();
    }   

}
执行脚本时,网站打开,但“userNameTextbox”页面对象对象获取错误空指针异常。 我使用的是selenium web驱动程序2.53.0。
请帮助我解决此问题。

尝试使用公共静态WebDriver;在所有地方,您的代码中都有许多问题,但第一个问题是:
@BeforeClass
必须是静态的,并且在实例化类之前被调用。然后在
@BeforeClass
方法中调用
browser
,该方法仅在类需要回复kiril时才会实例化。请您用一个例子解释一下。感谢Murali的回复。但据我所知,static是一个关键字,用于使任何变量、方法或类成为唯一值,在一个类中只定义一次。例如,拼贴所有员工的名称。