我遇到以下错误,如果有人可以告诉我我在做什么错,我将不胜感激。我正在尝试在navigationAllMenus.java中运行测试,但是一旦脚本尝试找到loginLink,它就会停止并在第28行中给我java.lang.NullPointerException:
_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("[email protected]");
        _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();
    }

}


对象loginLink似乎给我带来了麻烦:

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实例,即driver并且没有编写constructor。因此,您必须在_a_LoginPage类中添加以下内容:

//initialize the WebDriver instance
WebDriver driver;

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

09-12 04:24