使用Selenium和Page Object Pattern的TestNG NullPointerException

问题描述 投票:0回答:3

在尝试通过扩展抽象类和测试类来创建测试时,我得到NullPointerException。错误:

位于sun.reflect.NativeMethodAccessorImpl.invoke的Sun.reflect.NativeMethodAccessorImpl.invoke0(本地方法)处的UserRegistrationpageTest.fillName(UserRegistrationpageTest.java:26)处的pages.UserRegistrationPage.fillName(UserRegistrationPage.java:61)处的java.lang.NullPointerException( NativeMethodAccessorImpl.java:62)atg.test.invokeMinod.InvokeMethod(MethodInvocationHelper .java:124)org.testng.internal.InvokeMinod上的org.testng.internal.Invoker.invokeMethod(Invoker.java:571)atg.testng.internal.InvokeTestMethod(Invoker.java:707)at org.testng.internal.Invoker.invokeTestMethods( Invoker.java:979)在org.testng.TestRunner.privateRun上的org.testng.TestMethodWorker.run(TestMethodWorker.java:109)的org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)上。(TestRunner) .java:648)org.testng.TestRunner.run(TestRunner.java) :505)org.testng.SuiteRunner.runTest(SuiteRunner.java:455)org.testng.SuiteRunner.runSequentially(SuiteRunner.java:450)atg.testng.SuiteRunner.privateRun(SuiteRunner.java:415)at org位于org.testng.TunNite的org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:84)org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)的.testng.SuiteRunner.run(SuiteRunner.java:364)。 runOitesSequentially(TestNG.java:1187)org.testng.TestNG.runSuitesLocally(TestNG.java:1116)atg.testng.TestNG.runSuites(TestNG.java:1028)atg.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");
    }

}

我在这里缺少什么?

java selenium testng
3个回答
2
投票

原因是:当您到达'userRegistrationPage.fillName(“John”)'行时,'UserRegistration'页面的'driver'成员保持为空。

看到这个

enter image description here

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

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

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

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

private UserRegistrationPage userRegistrationPage = new UserRegistrationPage(driver);

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

enter image description here


0
投票

使用'PageFactory'时,您需要初始化WebElements。将其添加到'AbstractPage'

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

0
投票

扩展UserRegistrationPageAbstractPage使用WebDriver实例driver。因此,您需要将构造函数定义为:

WebDriver driver;

//constructor
public UserRegistrationPage(WebDriver driver)
{
    this.driver=driver;
}
© www.soinside.com 2019 - 2024. All rights reserved.