gpt4 book ai didi

java - 使用 Selenium 和页面对象模式进行 TestNG NullPointerException

转载 作者:行者123 更新时间:2023-12-02 02:24:58 25 4
gpt4 key购买 nike

尝试通过扩展抽象类和测试类来创建测试时,出现 NullPointerException。错误:

java.lang.NullPointerException at pages.UserRegistrationPage.fillName(UserRegistrationPage.java:61) at UserRegistrationpageTest.fillName(UserRegistrationpageTest.java:26) 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:124) at org.testng.internal.Invoker.invokeMethod(Invoker.java:571) at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:707) at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:979) 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:648) at org.testng.TestRunner.run(TestRunner.java:505) at org.testng.SuiteRunner.runTest(SuiteRunner.java:455) at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:450) at org.testng.SuiteRunner.privateRun(SuiteRunner.java:415) at org.testng.SuiteRunner.run(SuiteRunner.java:364) at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52) at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:84) at org.testng.TestNG.runSuitesSequentially(TestNG.java:1187) at org.testng.TestNG.runSuitesLocally(TestNG.java:1116) at org.testng.TestNG.runSuites(TestNG.java:1028) at org.testng.TestNG.run(TestNG.java:996) at org.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:72) at org.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:123)

驱动程序被初始化,打开测试页面,并在尝试填写字段时抛出异常。我的类(class):

    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。

看这个

enter image description here

这是因为@BeforeSuite(位于“AbstractTest”测试中的方法在调用 UserRegistrationPage 的构造函数之后被调用。所以现在的顺序是

1) AbstractPage 的构造函数。 (这里你的驱动程序为空)

2) UserRegistrationPage 的构造函数。 (这里你的驱动程序为空)

3) AbstractTest 的 @BeforeSuite。 (这里您的驱动程序不会为 null ,但直到此时您已经使用“UserRegistrationpageTest”类中​​的行初始化了“UserRegistrationPage”对象

private UserRegistrationPage userRegistrationPage = new UserRegistrationPage(driver);

要解决此问题,请更改“openURL”内“UserRegistrationPage”对象的初始化位置,如下所示

enter image description here

关于java - 使用 Selenium 和页面对象模式进行 TestNG NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47924561/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com