0

I implemented a single PageFactory BasePage class that holds the weblements for a test page. I created a constructor in BasePage for WebDriver driver reference

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

public class BasePage {
  WebDriver driver;

public BasePage(WebDriver driver) {      //constructor
    this.driver = driver;
  }


@FindBy(xpath = "path")
public WebElement id;
@FindBy(xpath = "path")
public WebElement password;
@FindBy(xpath = "path")
public WebElement signIn;

I then initialised the BasePage class in inside the test class, this way: BasePage basePage = PageFactory.initElements(driver, BasePage.class);The initialisation throws no error.

import org.testng.annotations.Test;

public class Active {
public static WebDriver driver;
private final BaseUtil baseUtil = new BaseUtil();
BasePage basePage = PageFactory.initElements(driver, BasePage.class); //Initialisation


@Test
public void navigateToUrl() throws InterruptedException {
ChromeDriverManager.getInstance().setup();
driver = new ChromeDriver()
driver.get(baseUtil.getUrl());

basePage.id.click();
basePage.id.sendKeys("0000");
basePage.password.click();
basePage.password.sendKeys("admin");
basePage.signIn.click();

}

I ran the test, but it could not execute, throwing a NullpointerException:

java.lang.NullPointerException
    at org.openqa.selenium.support.pagefactory.DefaultElementLocator.findElement(DefaultElementLocator.java:69)
    at org.openqa.selenium.support.pagefactory.internal.LocatingElementHandler.invoke(LocatingElementHandler.java:38)
    at com.sun.proxy.$Proxy6.click(Unknown Source)
    at Service.Active.navigateToUrl(Active.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.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:86)
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:643)
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:820)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1128)
    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:782)
    at org.testng.TestRunner.run(TestRunner.java:632)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:366)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:361)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:319)
    at org.testng.SuiteRunner.run(SuiteRunner.java:268)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1244)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1169)
    at org.testng.TestNG.run(TestNG.java:1064)
    at org.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:72)
    at org.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:123)

Does anyone know what I have done wrong with the synchronisation of PageFactory with my test class?

6
  • Move the BasePage initialization inside the test method after driver is initialized. Basically the driver is null in the current code. Commented May 9, 2018 at 10:17
  • @Grasshopper, brilliant. Easy does it. It works. If you put the answer in an answer box, I can mark it as the official answer Commented May 9, 2018 at 10:49
  • @Grasshopper, with just a single method in the 'Active.java' class, the initialisation works great as you described. I have decided to add multiple methods, how can I make the driver global, instead of initialising every method? Currently, it can only be seen by one method. Adding the initialisation at the class level as I described in the codes above throws 'NullpointerException' Commented May 9, 2018 at 13:56
  • Use the BeforeClass method to initialize the driver. THen it will be available in all test methods. Also no need for making the driver static in Active class testng.org/doc/documentation-main.html#annotations Commented May 9, 2018 at 14:04
  • Thanks. I replaced the Test annotation with BeforeClass and added the initialisation first at class level, got Nullpointer and then added the initialisation also to the BeforeClass method. Still got Nullpointer. Brower launched successfully, but no other method executed Commented May 9, 2018 at 14:24

1 Answer 1

1

Move the BasePage initialization inside the test method after driver is initialized. Basically the driver is null in the current code.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.