测试执行期间发生NullPointerException

时间:2019-03-29 10:26:52

标签: java selenium-webdriver nullpointerexception selenium-chromedriver

我是自动化测试的初学者。我创建了要登录到Web应用程序(LogInTest.java)的测试。目前,我在识别客户时遇到问题。这是另一个我有空指针异常的测试。我不知道我在做什么错。在我的代码下面。有人可以解释一下为什么这行不通吗?

package com.deviniti.ufe.nss.test.view360.Pages;

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

public class PageObjects {

    private WebDriver driver;
    private By login = By.id("username");
    private By password = By.id("password");
    private By loginClick = By.id("kc-login");
    private By skipClick = By.id("kc-skip");
    private By pesel = By.xpath("//input[@placeholder='Wpisz PESEL']");

    public PageObjects(WebDriver driver) {
        this.driver = driver;
    }

    public void setLogin(String strLogin) {
        driver.findElement(login).sendKeys(strLogin);
    }

    public void setPassword(String strPassword) {
        driver.findElement(password).sendKeys(strPassword);
    }

    public void login() {
        driver.findElement(loginClick).click();
    }

    public void setSkipClick() {
        driver.findElement(skipClick).click();
    }

    public void loginTo(String strLogin, String strPassword) {
        this.setLogin(strLogin);
        this.setPassword(strPassword);
        this.login();
        this.setSkipClick();
    }

    public void Pesel(String strPesel) {
        driver.findElement(pesel).sendKeys(strPesel);
    }

    public void CreatePesel(String strPesel) {
        this.Pesel(strPesel);
    }


}

此testLoginPage()正确

package com.deviniti.ufe.nss.test.view360.Tests;

import com.deviniti.ufe.nss.test.view360.Pages.PageObjects;
import com.deviniti.ufe.nss.test.view360.Pages.MainPage;
import com.deviniti.ufe.nss.test.view360.Pages.PageObjects;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class LogInTest {

    public WebDriver driver;
    PageObjects lp;


    @BeforeTest
    public void setup() {
        System.setProperty("webdriver.chrome.driver", "C:\\Users\\smartyn\\Dysk Google\\all\\testowanie\\chromedriver_win32\\chromedriver.exe");
        driver = new ChromeDriver();
        driver.get("https://ufe-ramka-app.p24.openshift.creditagricole/");
        driver.manage().window().maximize();
    }

    /* @AfterTest
    public void tearDown() {
        driver.quit();
    } */

    @Test(priority = 0)
    public void testLoginPage() {
        lp = new PageObjects(driver);
        lp.loginTo("DevinitiPB", "DevinitiPB1");
    }

}

这里我遇到了Identification():(-nullpointerexception

package com.deviniti.ufe.nss.test.view360.Tests;

import com.deviniti.ufe.nss.test.view360.Pages.PageObjects;
import com.deviniti.ufe.nss.test.view360.Pages.MainPage;
import com.deviniti.ufe.nss.test.view360.Pages.PageObjects;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.BeforeTest;

public class IdentificationWorkWithClient {

    public WebDriver driver;
    LogInTest lt;
    PageObjects po;


    @BeforeTest
    public void setup() {
        System.setProperty("webdriver.chrome.driver", "C:\\Users\\smartyn\\Dysk Google\\all\\testowanie\\chromedriver_win32\\chromedriver.exe");
        driver = new ChromeDriver();
        driver.get("https://ufe-ramka-app.p24.openshift.creditagricole/");
        driver.manage().window().maximize();

    }

    @Test
    public void Identification() {
        lt.testLoginPage();
        po.CreatePesel("61121211799");
    }
}

java.lang.NullPointerException
    at com.deviniti.ufe.nss.test.view360.Tests.IdentificationWorkWithClient.Identification(IdentificationWorkWithClient.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.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.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

1 个答案:

答案 0 :(得分:0)

@Test
public void Identification() {
    lt.testLoginPage();
    po.CreatePesel("61121211799");
}

在这里,您尝试在未实例化的对象上调用方法。
确保您初始化lt和po。