gpt4 book ai didi

java - 如何通过 中的调用解决 webdriver 设置中的 NullPointerException?

转载 作者:行者123 更新时间:2023-11-30 06:59:18 26 4
gpt4 key购买 nike

public class MainPage extends BasePage<MainPage> {

public MainPage(WebDriver driver) {

super(driver);

}

WebDriverWait wait = new WebDriverWait(driver, WAIT_TIME);

public LoginPage clickLoginButton() {

wait.until(ExpectedConditions.elementToBeClickable(By.linkText("Login")));

WebElement GoElement = driver.findElement(By.linkText("Login"));

GoElement.click();

return new LoginPage(driver);

}
}

// This MainPage mistake-->WebDriverWait wait = new
// WebDriverWait(driver, WAIT_TIME);

public class UserPage extends BasePage<UserPage> {

public UserPage(WebDriver driver) {

super(driver);
}

WebDriverWait wait = new WebDriverWait(driver, WAIT_TIME);

public UserPage clickUpdatePasswordButton() throws InterruptedException {
driver.findElement(By.linkText("Update")).click();

Thread.sleep(2000);

return this;
}
}

//该用户页面

public class UserTest extends BaseTest {

@Test
public void UserControl() throws InterruptedException {

new MainPage(driver).clickLoginButton().loginSuccessful("agasgasgasga@gmail.com", "1212121211").checkMainPage().clickToAccount();
}
}

代码错误:

java.lang.NullPointerException
at com.google.common.base.Preconditions.checkNotNull(Preconditions.java:212)
at org.openqa.selenium.support.ui.FluentWait.<init>(FluentWait.java:102)
at org.openqa.selenium.support.ui.WebDriverWait.<init>(WebDriverWait.java:71)
at org.openqa.selenium.support.ui.WebDriverWait.<init>(WebDriverWait.java:45)
at page.MainPage.<init>(MainPage.java:20)
at test.UserTest.UserControl(UserTest.java:16)
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.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:117)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:42)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:262)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:84)
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 com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)


Process finished with exit code 255



This show mistake

MainPage --> WebDriverWait wait = new WebDriverWait(driver,
WAIT_TIME);

最佳答案

您需要初始化驱动程序

public class BaseTest {
protected WebDriver driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}

关于java - 如何通过 <init> 中的调用解决 webdriver 设置中的 NullPointerException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41207400/

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