带有PageFactory的Selenium java.lang.NullPointerException

时间:2016-07-04 12:58:07

标签: java selenium nullpointerexception

我试图抓住PageFactory POM,但是有些东西不起作用,我无法理解错误。

这是我的第一个主页POM课程:

package PageFactory;

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

public class Home_Page_POF {

public WebDriver driver;

@FindBy(css = "div#header-profile a#header-profile-toggle")
public WebElement profileToggleButton;

@FindBy(css = "form#loginUserdataForm div.footer div.add-footer a.btn.btn-link.linkicon")
public WebElement newRegistrationButton;

public Home_Page_POF(WebDriver driver) {
this.driver = driver;

PageFactory.initElements(driver, this);
}

}

这是Reg的第二个POM课程。页

package PageFactory;

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


public class Registration_Page_POF {

public WebDriver driver;

//Personal details WebElements
@FindBy(css = "form#personalDetailsForm div.profile.pe div.profile-block.simple.first fieldset#pefield-title select#pesalutation")
public WebElement titleDropdown;
public Select titleSelect = new Select(titleDropdown);

public Registration_Page_POF(WebDriver driver) {
this.driver = driver;

PageFactory.initElements(driver, this);
}

}

这是测试用例:

package Tests;

import PageFactory.Home_Page_POF;
import PageFactory.Registration_Page_POF;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.io.TemporaryFilesystem;
import org.openqa.selenium.support.ui.Select;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import java.util.concurrent.TimeUnit;

public class Test_POF {

public WebDriver driver;

Home_Page_POF objHomePage;
Registration_Page_POF objRegPage;

@BeforeClass
public void browserSetUp() {
    System.setProperty("webdriver.chrome.driver", "D:/Install/selenium-2.53.0/drivers/chromedriver.exe");
    driver = new ChromeDriver();

    driver.manage().window().maximize();
    driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);

    driver.navigate().to("http://www.lufthansa.com/");        
}

@AfterClass
public void broserCleanUp() {
    if (driver != null) {
        TemporaryFilesystem.getDefaultTmpFS().deleteTemporaryFiles();
        driver.close();
        driver.quit();
    }
}

@Test
public void Test0001() {

    objHomePage = new Home_Page_POF(driver);

    objHomePage.profileToggleButton.click();

    objHomePage.newRegistrationButton.click();

    objRegPage = new Registration_Page_POF(driver);

    Select titleSelect = new Select(objRegPage.titleDropdown);

    titleSelect.selectByVisibleText("Mr.");

}
}

所以HomePage对象工作正常,我点击两个按钮进入注册页面。 之后我想从下拉列表中选择一个值,但它给了我NullPointerException:

java.lang.NullPointerException
at org.openqa.selenium.support.ui.Select.<init>(Select.java:44)
at PageFactory.Registration_Page_POF.<init>(Registration_Page_POF.java:17)
at Tests.Test_POF.Test0001(Test_POF.java:56)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:85)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:639)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:816)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1124)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:108)
at org.testng.TestRunner.privateRun(TestRunner.java:774)
at org.testng.TestRunner.run(TestRunner.java:624)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:359)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:354)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:312)
at org.testng.SuiteRunner.run(SuiteRunner.java:261)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1215)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1140)
at org.testng.TestNG.run(TestNG.java:1048)
at org.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:74)
at org.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:121)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)

1 个答案:

答案 0 :(得分:1)

问题是您正在初始化类本身中的变量titleSelect。 Just Initialize内部构造函数或找到元素之后;

public class Registration_Page_POF {

public WebDriver driver;

//Personal details WebElements
@FindBy(css = "form#personalDetailsForm div.profile.pe div.profile-block.simple.first fieldset#pefield-title select#pesalutation")
public WebElement titleDropdown;
public Select titleSelect; //Dont initialize here

public Registration_Page_POF(WebDriver driver) {
this.driver = driver;

PageFactory.initElements(driver, this);
titleSelect = new Select(titleDropdown);//initialize here
}

}