0

I have a page object with im trying to instantiate so that I can perform methods on it in my tests like goto etc.

But when I try to use it with PageFactory.initElements(driver,this);. The browser only opens when invoked with my go-to the method and then immediately quits with a nullpointer error.

If I use this.driver = driver. It works as intended. So the work around is to call both methods.. but that's duplicating my code...

public Homepage(WebDriver driver)
    {
        this.driver = driver;
        PageFactory.initElements(driver,this);
    }

Here is my simple test method.

public void Goto()
{
    System.out.println("Navigating");
    driver.get("https://www.practiceground.org/");
}

And my actual test.

@Test
    public void Test_All_The_Navbar_Links()
    {
        var homepage = new Homepage(driver);

        homepage.Goto();
    }

Edit:

Stacktrack as requested.

java.lang.NullPointerException
    at PageObjects.Homepage.Goto(Homepage.java:25)
    at FirstTestCase.Test_All_The_Navbar_Links(FirstTestCase.java:11)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:567)
    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.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
    at org.junit.rules.TestWatcher$1.evaluate(TestWatcher.java:55)
    at org.junit.rules.TestWatcher$1.evaluate(TestWatcher.java:55)
    at org.junit.rules.RunRules.evaluate(RunRules.java:20)
    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)
  • Can you please paste full stacktrace? – dzieciou Nov 8 at 22:46
  • Post edited with it included. – Keva161 Nov 8 at 22:51
  • PageFactory.init() takes PageObject class and return its instance. Instead you are passing an instace (this) and ignoring the result. – dzieciou Nov 8 at 22:59
  • Sorted it. I created a init method that calls homepage = PageFactory.initElements(driver, Homepage.class); and then use this.driver = driver; in my page class. I would prefer to pass driver to my page once. But this seems to work. – Keva161 Nov 9 at 12:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.