我实现了一个单个PageFactory BasePage类,该类包含测试页面的weblements。我在BasePage中创建了一个构造函数以供WebDriver驱动程序参考

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

public class BasePage {
  WebDriver driver;

public BasePage(WebDriver driver) {      //constructor
    this.driver = driver;
  }


@FindBy(xpath = "path")
public WebElement id;
@FindBy(xpath = "path")
public WebElement password;
@FindBy(xpath = "path")
public WebElement signIn;


然后,我以这种方式在测试类内部初始化BasePage类:BasePage basePage = PageFactory.initElements(driver, BasePage.class);初始化未引发任何错误。

import org.testng.annotations.Test;

public class Active {
public static WebDriver driver;
private final BaseUtil baseUtil = new BaseUtil();
BasePage basePage = PageFactory.initElements(driver, BasePage.class); //Initialisation


@Test
public void navigateToUrl() throws InterruptedException {
ChromeDriverManager.getInstance().setup();
driver = new ChromeDriver()
driver.get(baseUtil.getUrl());

basePage.id.click();
basePage.id.sendKeys("0000");
basePage.password.click();
basePage.password.sendKeys("admin");
basePage.signIn.click();

}


我运行了测试,但无法执行,抛出NullpointerException:

java.lang.NullPointerException
    at org.openqa.selenium.support.pagefactory.DefaultElementLocator.findElement(DefaultElementLocator.java:69)
    at org.openqa.selenium.support.pagefactory.internal.LocatingElementHandler.invoke(LocatingElementHandler.java:38)
    at com.sun.proxy.$Proxy6.click(Unknown Source)
    at Service.Active.navigateToUrl(Active.java:30)
    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:86)
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:643)
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:820)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1128)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:129)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:112)
    at org.testng.TestRunner.privateRun(TestRunner.java:782)
    at org.testng.TestRunner.run(TestRunner.java:632)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:366)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:361)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:319)
    at org.testng.SuiteRunner.run(SuiteRunner.java:268)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1244)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1169)
    at org.testng.TestNG.run(TestNG.java:1064)
    at org.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:72)
    at org.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:123)


有人知道我将PageFactory与测试类同步时做错了什么吗?

最佳答案

初始化驱动程序后,将BasePage初始化移至测试方法内。基本上,驱动程序在当前代码中为null。

07-26 09:08