使用Selenium和Page Object Pattern

时间:2017-12-21 12:00:54

标签: java selenium testng

在尝试通过扩展抽象类和测试类来创建测试时,我得到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)     在   sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)     在java.lang.reflect.Method.invoke(Method.java:498)at   org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:124)     在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)     在   org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)     在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 at   org.testng.SuiteRunner.privateRun(SuiteRunner.java:415)at 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 at   org.testng.TestNG.runSuitesLocally(TestNG.java:1116)at 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 at   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");
    }

}

我在这里缺少什么?

3 个答案:

答案 0 :(得分:2)

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

看到这个

enter image description here

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

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

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

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

private UserRegistrationPage userRegistrationPage = new UserRegistrationPage(driver);

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

enter image description here

答案 1 :(得分:0)

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

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

答案 2 :(得分:0)

扩展UserRegistrationPage的{​​{1}}使用AbstractPage实例 WebDriver 。因此,您需要将构造函数定义为:

driver