简体   繁体   中英

Getting Null pointer exception when running Selenium through TestNG

I have Test base class which calls a method from LandingPage class which has the locator for the page under test. When I execute this project, I'm getting Null pointer exception. I'm sure it has something to do with testNG annotations, but I'm unable to find out the reason.

package com.xyz.tests;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

import com.xyz.pageObjects.LandingPage;
import com.xyz.pageObjects.LoginPage;
import com.xyz.utils.Utils;

import engine.Engine;

public class LandingPageTest {

    public WebDriver driver;
    //Engine engine = new Engine(driver);
    LandingPage landingPage;
    LoginPage loginPage;

    @BeforeTest
    public void setUp(){

        driver = new FirefoxDriver();
        driver.manage().window().maximize();
        driver.get("https://www.go.com");
        driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
    }

    @Test
    public void clickOnSignInLink() {

    landingPage.SignIn().click();

    }

Below is my LandingPage class

 package com.xyz.pageObjects;

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


public class LandingPage {

    private WebDriver driver;

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

    //By signIn= By.xpath(".//*[@id='pageContainerInner']/div[2]/div[1]/div/div[2]/div[1]/a");

    By signInBtn = By.linkText("Sign In or Create Account");
    //By signInBtn2 = By.cssSelector("css=a.signIn");

    By closeCrisisMessage = By.xpath(".//*[@id='closeCrisisMessageBtn']");
        public WebElement SignIn(){
            return driver.findElement(signInBtn);
    }
}

Following is the error message

 C:\Users\ad\AppData\Local\Temp\testng-eclipse--957796922\testng-customsuite.xml

FAILED: clickOnSignInLink
java.lang.NullPointerException
    at com.disney.tests.LandingPageTest.clickOnSignInLink(LandingPageTest.java:36)
    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.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:84)
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:714)
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
    at org.testng.TestRunner.privateRun(TestRunner.java:767)
    at org.testng.TestRunner.run(TestRunner.java:617)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
    at org.testng.SuiteRunner.run(SuiteRunner.java:240)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1224)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1149)
    at org.testng.TestNG.run(TestNG.java:1057)
    at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)

Any suggestions will be appreciated.

我不确定在什么行上抛出NullPointerException,但是我希望这会失败,因为在运行@Test方法时,您的着陆页尚未初始化:

landingPage = new LandingPage(driver);

I see, you didn't instantiate landingPage & loginPage anywhere. That's why you are getting a null pointer exception. To get around this issue, you have to instantiate those references (mainly, landingPage here). Instantiate it either in place of declaration or in @BeforeClass annotation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM