我具有以下硒结构,启动并最大化浏览器后遇到错误。当@Test方法开始执行时,我正在获取Java空指针异常。

请提出解决方案

基类:

public class BaseClass

{


    public static WebDriver driver ;

    public BaseClass(WebDriver _driver)
    {
        driver = _driver;
    }

    public  WebElement FirstName = driver.findElement(By.name("firstname"));
    public  WebElement LastName  = driver.findElement(By.name("LastName"));




    public String GetFirstName()
    {
        return FirstName.getText();

    }

    public String GetLastName()
    {
        return LastName.getText();
    }


    public WebDriver  SetFirstname(String FirstName)
    {
        this.FirstName.sendKeys(FirstName);
        return driver;
    }

    public WebDriver SetLastname(String LastName)
    {
        this.LastName.sendKeys(LastName);
        return driver;
    }

}


测试类别:

public class TestClassTest

{

     public static WebDriver driver =null;
     private String Url ="";
     private String DriverOption="";
     private String DriverPath="";

     private String Firstname= "";
     private String LastName= "";



    @BeforeSuite(alwaysRun = true)
    public void setupBeforeSuite(ITestContext context) throws IOException
     {

     Url = context.getCurrentXmlTest().getParameter("URL");

     DriverOption = context.getCurrentXmlTest().getParameter("DriverOptions");

     DriverPath = context.getCurrentXmlTest().getParameter("DriverPath");


     Firstname= ReadValue_XL(driver,0,0);

     LastName= ReadValue_XL(driver,0,1);

     }

    @Parameters("Browser")
    @BeforeTest
    public void SetUp(String Browser)
    {
        if(Browser.equalsIgnoreCase("chrome"))
        {
        System.setProperty(DriverOption,DriverPath);

        driver = new ChromeDriver();
        }

        if(Browser.equalsIgnoreCase("firefox"))
        {
        System.setProperty(DriverOption, DriverPath);

        driver = new FirefoxDriver();
        }

        driver.get(Url);

        driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);

        driver.manage().window().maximize();



    }


    @Test (priority = 1)
    public void TestOne()
    {
        BaseClass Base= new BaseClass(driver);

        Base.SetFirstname(Firstname);
        Base.SetLastname(LastName);

        Assert.assertEquals(Base.GetFirstName(), "Dog Food");


    }



    @AfterTest
    public void Driver_close()
    {
        driver.close();
        driver.quit();
    }


    public String ReadValue_XL(WebDriver driver,int Row,int Column) throws IOException
    {
        XSSFWorkbook WrkBk = new XSSFWorkbook(".\\Input\\Input.xlsx");

        XSSFSheet sourceSheet = WrkBk.getSheet("Sheet1");

        XSSFRow sourceRow = sourceSheet.getRow(Row);

        XSSFCell cell1=sourceRow.getCell(Column);

        cell1.toString();

        WrkBk.close();

        return cell1.toString();

    }
}


错误:

java.lang.NullPointerException
    at baseProject.BaseClass.<init>(BaseClass.java:19)
    at TestProject.TestClassTest.TestOne(TestClassTest.java:88)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:108)
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:669)
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:877)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1201)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
    at org.testng.TestRunner.privateRun(TestRunner.java:776)
    at org.testng.TestRunner.run(TestRunner.java:634)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:425)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:420)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:385)
    at org.testng.SuiteRunner.run(SuiteRunner.java:334)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1318)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1243)
    at org.testng.TestNG.runSuites(TestNG.java:1161)
    at org.testng.TestNG.run(TestNG.java:1129)
    at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:114)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)


我能够理解,驱动程序未在基类中正确解析或初始化。请提出我必须解决此问题的地方。

当我在testclass中扩展基类时,没有TestNG类正在运行。

最佳答案

初始化程序在构造函数之前被调用,因此NullPointerException尝试以下代码:

public class BaseClass{

  public static WebDriver driver;

  public BaseClass(WebDriver _driver){
      driver = _driver;
      FirstName = driver.findElement(By.name("firstname"));
      LastName  = driver.findElement(By.name("LastName"));
  }

  public  WebElement FirstName;
  public  WebElement LastName;
  //Rest of the code

10-01 02:50