jeffsia
jeffsia

Reputation: 369

Null pointer exception when calling a Page Object Model class

I am trying my hands on Selenium test automation using TestNG as the test framework. For this, I have used a Page Object pattern to model each of the page of the website that I am writing the test for.

I have two java classes

  1. SignInPage.java (Page Object Model Class)
  2. TestLogin.java (where the actual test is written and where the SignInPage is instantiated)

SignInPage.java

public class SignInPage {
private WebDriver driver;

@FindBy(id = "username")
private WebElement usernameTextbox;

@FindBy(id = "password")
private  WebElement passwordTextbox;

@FindBy(xpath = "//*[@id=\"left\"]/div[3]/div/form/input[3]")
private WebElement loginButton;

public SignInPage(WebDriver driver) {
    this.driver = driver;
    driver.get("www.somewebsite.com");
}

public HomePage performLogin(String username, String password){
    usernameTextbox.sendKeys(username);
    passwordTextbox.sendKeys(password);
    loginButton.click();
    return PageFactory.initElements(driver, HomePage.class);
}
}

TestLogin.java

public class TestLogin {
public WebDriver driver;

@BeforeClass
public void setup(){
    System.setProperty("webdriver.chrome.driver", "path_to_chromedriver.exe");
    driver = new ChromeDriver();
}

@Test
public void TestSignIn(){
    SignInPage signInPage = new SignInPage(driver);
    HomePage homePage = signInPage.performLogin("someusername","somepassword");
}
}

My problem is whenever the signInPage.performLogin() is invoked in the @Test of TestLogin.java, a null pointer exception is encountered.

java.lang.NullPointerException
at com.tipidpc.webpages.SignInPage.performLogin(SignInPage.java:30)
at com.tipidpc.tests.TestLogin.TestSignIn(TestLogin.java:26)
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.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:104)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:645)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:851)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1177)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:129)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:112)
at org.testng.TestRunner.privateRun(TestRunner.java:756)
at org.testng.TestRunner.run(TestRunner.java:610)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:387)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:382)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:340)
at org.testng.SuiteRunner.run(SuiteRunner.java:289)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1293)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1218)
at org.testng.TestNG.runSuites(TestNG.java:1133)
at org.testng.TestNG.run(TestNG.java:1104)
at org.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:72)
at org.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:127)
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 com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)

Upvotes: 0

Views: 2054

Answers (1)

jeffsia
jeffsia

Reputation: 369

I solved the issue. Since I was using the TestNG framework to run my tests, the proper imports must be present for the test script to run properly.

All my driver initialization is being done in the @BeforeClass method but it uses the import org.junit.BeforeClass. The solution was to change this to org.testng.annotations.BeforeClass.

Upvotes: 1

Related Questions