NullPointerException during test execution

问题: I am beginner in automation tests. I created test which is logging into web application (LogInTest.java). Currently I have problem during customer identification. This is a...

问题:

I am beginner in automation tests. I created test which is logging into web application (LogInTest.java). Currently I have problem during customer identification. This is another test where I have null pointer exception. I don't have idea what I'm doing wrong. Below my codes. Can someone explain me why this doesn't work?

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);
    }


}

This testLoginPage() is correctly

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");
    }

}

Here I have problem 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:

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

Here you try to invoke a method on an object that does not appear to have been instantiated.
Ensure you initialise lt and po.

  • 发表于 2019-03-31 02:13
  • 阅读 ( 235 )
  • 分类:sof

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除