0

Created base class:

public loginpage() {           
    PageFactory.initElements(driver,this);
}

Then page objects initialized:

public String GetTitle() {
    System.out.println("title111");
    return driver.getTitle();

Then created test case:

@Test
    public void title() {
    String a=   lp.GetTitle();
    System.out.println(a);
    }

Output:

java.lang.NullPointerException: Cannot invoke "org.openqa.selenium.WebDriver.getTitle()" because "this.driver" is null

Please let me know what's wrong.

monojohnny
  • 5,710
  • 15
  • 57
  • 82

1 Answers1

0

To use the same browser created within the testcase or in the Browser Factory needs to be reused.

So within the Page Objects first you need to:

public LoginPage(WebDriver loginPageDriver) {
    this.driver=loginPageDriver;
}

and then:

public String GetTitle() {
    System.out.println("title111");
    return driver.getTitle();
}

References

You can find a couple of relevant detailed discussions in:

undetected Selenium
  • 151,581
  • 34
  • 225
  • 281