我实现了一个单独的PageFactory BasePage类,用于保存测试页面的碎片。我在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与我的测试类同步时做错了什么?
答案 0 :(得分:1)
初始化驱动程序后,在测试方法中移动BasePage
初始化。基本上,当前代码中的驱动程序为空。