gpt4 book ai didi

java - selenium PageFactory 与测试类同步失败

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

我实现了一个 PageFactory BasePage 类,它保存测试页面的 webements。我在 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 初始化移至测试方法内。基本上当前代码中的驱动程序为空。

关于java - selenium PageFactory 与测试类同步失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50250662/

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