POM模型中的NullPointerException

时间:2017-10-21 14:30:48

标签: java maven junit nullpointerexception pageobjects

我在下面的代码中面临NullPointerException - 它是在POM模型中创建的项目。 我正在阅读与NullPointerException相关的其他帖子,但我无法在我的Java代码中找到错误。 感谢任何帮助。

测试类:

package pl.b2b.ProjectAutomationPractice;

import static org.junit.Assert.assertEquals;
import org.junit.Test;

public class AutomationPracticeTest extends Utils {

LoginPage LoginPage = new LoginPage();
DefaultPage DefaultPage = new DefaultPage();

@Test
public void incorrectEmailLoginPage() {
    DefaultPage.clickButtonSignIn();
    LoginPage.enterEmail("xxx");
    LoginPage.enterPassword("xxx");
    assertEquals("There is 1 error\n" + "Invalid email address.", LoginPage.getWarningIncorrectLoginDetails());
}

@Test
public void incorrectPasswordLoginPage() {
    DefaultPage.clickButtonSignIn();
    LoginPage.enterEmail("xxx");
    LoginPage.enterPassword("xxx");
    assertEquals("There is 1 error\n" + "Authentication failed.", LoginPage.getWarningIncorrectLoginDetails());
}
}

页面类:

package pl.b2b.ProjectAutomationPractice;

import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;

public class LoginPage {

@FindBy(id = "email")
private WebElement fieldEmailLogin;

@FindBy(id = "passwd")
private WebElement fieldPassword;

@FindBy(xpath = "//*[@id=\\\"SubmitLogin\\\"]/span")
private WebElement buttonSignInAfterEnteringLoginDetails;

@FindBy(xpath = "//*[@id=\\\"searchbox\\\"]/button")
private WebElement buttonSearchMagnifyingGlass;

@FindBy(xpath = "//*[@id=\\\"center_column\\\"]/p")
private WebElement messageSuccessfulLogin;

@FindBy(xpath = "//*[@id=\\\"center_column\\\"]/div[1]")
private WebElement warningIncorrectLoginDetails;



public String enterEmail(String email) {
    this.fieldEmailLogin.sendKeys(email);
    return email;
}

public String enterPassword(String password) {
    this.fieldPassword.sendKeys(password);
    return password;
}

public void clickButtonSignInAfterEnteringLoginDetails() {
    this.buttonSignInAfterEnteringLoginDetails.click();
}


public String getMessageSuccessfulLogin() {
    return this.messageSuccessfulLogin.getText().trim();
}

public String getWarningIncorrectLoginDetails() {
    return this.warningIncorrectLoginDetails.getText().trim();
}

}

其他网页类:

package pl.b2b.ProjectAutomationPractice;

import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;

public class DefaultPage {

@FindBy(xpath = "//*[@id=\\\"header\\\"]/div[2]/div/div/nav/div[1]/a")
private WebElement buttonSignIn;

public void clickButtonSignIn() {
    this.buttonSignIn.click();
}

}

方法:

package pl.b2b.ProjectAutomationPractice;

import org.junit.After;
import org.junit.Before;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class Utils {

private WebDriver driver;

@Before
public void createBrowserInstanceNavigateToURL() {
    System.setProperty("webdriver.chrome.driver", "C:\\chromedriver.exe");
    driver = new ChromeDriver();
    driver.get("http://automationpractice.com/index.php");
}

@After
public void close() {
    driver.close();
}
}

错误:

java.lang.NullPointerException
at pl.b2b.ProjectAutomationPractice.DefaultPage.clickButtonSignIn(DefaultPage.java:12)
at pl.b2b.ProjectAutomationPractice.AutomationPracticeTest.incorrectEmailLoginPage(AutomationPracticeTest.java:13)
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: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.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
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:539)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:761)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:461)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:207)

2 个答案:

答案 0 :(得分:0)

private WebElement buttonSignIn类中的

DefaultPage似乎没有被实例化。你的正则表达式可能会失败

答案 1 :(得分:0)

我正在尝试编辑我的代码 - 当我用xpath输入行到我的测试时,它完美地工作。当我尝试将相同的Xpath复制到DefaultPage类时会显示错误消息。不知道为什么。

public void incorrectEmailLoginPage() {
    driver.findElement(By.xpath("//*[@id='header']/div[2]/div/div/nav/div[1]/a")).click();
    DefaultPage.clickButtonSignIn();
    LoginPage.enterEmail("incorrectEmail@test.com");
    LoginPage.enterPassword("n3tw0rk2017");
    assertEquals("There is 1 error\n" + "Invalid email address.", LoginPage.getWarningIncorrectLoginDetails());