[英]TestNG NullPointerException with Selenium and Page Object Pattern
尝试通过扩展抽象类和测试类来创建测试时,出现NullPointerException异常。 错误:
位于java.lang.NullPointerException的pages.UserRegistrationPage.fillName(UserRegistrationPage.java:61)位于sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)处的UserRegistrationpageTest.fillName(UserRegistrationpageTest.java:26)在sun.reflect.NativeMethodAccessorImpl.invoke( org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper)上位于java.lang.reflect.Method.invoke(Method.java:498)上的sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)上的NativeMethodAccessorImpl.java:62) .java:124),位于org.testng.internal.Invoker.invokeMethod(Invoker.java:571),位于org.testng.internal.Invoker.invokeTestMethod(Invoker.java:707),位于org.testng.internal.Invoker.invokeTestMethods(在org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)处的Invoker.java:979)在org.testng.TestRunner.privateRun(TestRunner)处的org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)处.java:648),位于org.testng.TestRunner.run(TestRunner.java :505)在org.testng.SuiteRunner.runTest(SuiteRunner.java:455)在org.testng.SuiteRunner.runSequentially(SuiteRunner.java:450)在org.testng.SuiteRunner.privateRun(SuiteRunner.java:415)在org org.testng上的.testng.SuiteRunner.run(SuiteRunner.java:364).org上的.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:84)。 org.testng.TestNG.runSuitesSequentially(TestNG.java:1187)org.testng.TestNG.runSuites(TestNG.java:1028)org.testng.TestNG.run(TestNG.java) :996),位于org.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:72),位于org.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:123)
驱动程序被初始化,打开测试页,并在尝试填写该字段时引发异常。 我的课程:
public abstract class AbstractPage {
private WebDriver driver;
public AbstractPage(WebDriver driver){
this.driver = driver;
}
}
public class UserRegistrationPage extends AbstractPage{
@FindBy(id = "input-firstname")
private WebElement firstName;
@FindBy(id = "input-lastname")
private WebElement lastName;
@FindBy(id = "input-email")
private WebElement email;
@FindBy(id = "input-telephone")
private WebElement telephone;
@FindBy(xpath = "//*[@id=\"input-fax\"]")
private WebElement fax;
@FindBy(id = "input-address-1")
private WebElement address;
@FindBy(id = "input-city")
private WebElement city;
@FindBy(id = "input-postcode")
private WebElement postcode;
@FindBy(id = "input-country")
private WebElement country;
@FindBy(id = "input-zone")
private WebElement zone;
@FindBy(id = "input-password")
private WebElement password;
@FindBy(id = "input-confirm")
private WebElement passwordConfirm;
@FindBy(css = "#content input[type=\"checkbox\"]:nth-child(2)")
private WebElement agreeCheckbox;
@FindBy (xpath = "//*[@id=\"content\"]/form/div/div/input[2]")
private WebElement submitButton;
public UserRegistrationPage(WebDriver driver) {
super(driver);
}
public void fillName(String name){
this.firstName.sendKeys(name);
}
}
public abstract class AbstractTest {
protected WebDriver driver;
@BeforeSuite
public void setUpDriver(){
driver = new FirefoxDriver();
}
}
public class UserRegistrationpageTest extends AbstractTest{
private UserRegistrationPage userRegistrationPage = new UserRegistrationPage(driver);
@BeforeTest
void openURL(){
driver.get("http://88.119.151.54/opencartone/index.php?route=account/register");
}
@AfterTest
void closeBrowser(){
driver.quit();
}
@Test
void fillName(){
userRegistrationPage.fillName("John");
}
}
我在这里想念的是什么?
原因是:当到达“ userRegistrationPage.fillName(“ John”)”行时,“ UserRegistration”页面的“ driver”成员仍然为null。
看到这个
这是因为@BeforeSuite(位于“ AbstractTest”测试中的方法在UserRegistrationPage的构造函数被调用之后被调用。所以现在的顺序是
1)AbstractPage的构造方法。 (这里的驱动程序为空)
2)UserRegistrationPage的构造方法。 (这里的驱动程序为空)
3)@Test的@BeforeSuite。 (这里的驱动程序不会为null,但是直到现在,您已经使用'UserRegistrationpageTest'类中的行初始化了'UserRegistrationPage'对象
private UserRegistrationPage userRegistrationPage = new UserRegistrationPage(driver);
要解决此问题 ,请在“ openURL”中更改“ UserRegistrationPage”对象的初始化位置,如下所示
使用“ PageFactory”时,您需要初始化WebElements
。 将其添加到“ AbstractPage”
public AbstractPage(WebDriver driver) {
this.driver = driver;
PageFactory.initElements(driver, this);
}
扩展AbstractPage
的UserRegistrationPage
使用WebDriver
实例driver
。 因此,您需要将构造函数定义为:
WebDriver driver;
//constructor
public UserRegistrationPage(WebDriver driver)
{
this.driver=driver;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.