gpt4 book ai didi

java - Selenium java.lang.nullpointerexception JUnit 测试用例

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

我有以下错误,如果有人能告诉我我做错了什么,我将不胜感激。我正在尝试在 navigateAllMenus.java 中运行测试,但是当脚本尝试找到 loginLink 时,它会停止并给出 java.lang.NullPointerException em> 第 28 行:_a_LoginPage.loginLink(driver).click();.

我已经多次尝试对同一元素使用不同的 xpath,但没有任何效果。我处于初级水平,但仍然对某些事情感到困惑。这就是我所拥有的,提前谢谢您。

追踪:

java.lang.NullPointerException
at autFwk.navigateAllMenus.test(navigateAllMenus.java:28)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

测试用例:

package autFwk;

import static org.junit.Assert.*;

import org.junit.BeforeClass;
import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

import pagObj._b_HomePage;
import pagObj._a_LoginPage;

public class navigateAllMenus {

@BeforeClass
public static void setUpBeforeClass() throws Exception {
System.setProperty("webdriver.chrome.driver","ChromeDriver/chromedriver.exe");
}

@Test
public void test() {
WebDriver driver = new ChromeDriver();
driver.get("https://www.interaction-design.org/");
_a_LoginPage.loginLink(driver).click();
_a_LoginPage.email(driver).sendKeys("em@ail.com");
_a_LoginPage.password(driver).sendKeys("password");
_a_LoginPage.loginButton(driver).click();
_b_HomePage.Profile(driver).click();
driver.navigate().back();
_b_HomePage.Courses(driver).click();
driver.navigate().back();
_b_HomePage.Community(driver).click();
driver.navigate().back();
_b_HomePage.Logout(driver).click();
assertTrue(_a_LoginPage.loginLink(driver).isDisplayed());
driver.quit();
}

}

对象登录链接似乎给我带来了麻烦:

package pagObj;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

public class _a_LoginPage {

private static WebElement element = null;

public static WebElement loginLink (WebDriver driver){
driver.findElement(By.xpath("//a[@href='https://www.interaction-design.org/login']"));
return element;

最佳答案

您收到 NullPointerException 因为在 _a_LoginPage 类中您尚未初始化 WebDriver 实例,即驱动程序,并且尚未编写构造函数。因此,您必须在 _a_LoginPage 类中添加以下内容:

//initialize the WebDriver instance
WebDriver driver;

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

关于java - Selenium java.lang.nullpointerexception JUnit 测试用例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47151643/

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