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,894
  • 16
  • 59
  • 83
solveit
  • 1
  • 1

2 Answers2

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
  • 183,867
  • 41
  • 278
  • 352
0

WebDriver driver;

@BeforeMethod
public void setupMethod() throws InterruptedException 
{
    
    System.setProperty("webdriver.chrome.driver", "D:\\Driver\\chromedriver.exe");
    driver=new ChromeDriver();//launch chrome
    driver.manage().window().maximize();
    driver.manage().deleteAllCookies();
    driver.manage().timeouts().pageLoadTimeout(Duration.ofSeconds(50));//Wait for page to be loaded
    driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(50));//wait for element click and load(global)
    driver.get("https://www.facebook.com/");
    Thread.sleep(4000);
}


@Test
public void GoogleTitleTest()
{
    String title=driver.getTitle();
    System.out.println(title);
}

It will work there ,don't declare Webdriver=driver in @AfterMethod as it has been already declared before use in method.