I'm trying to build a Cucumber Java framework in Page Object Model. I have created the base framework and it works fine, but got confused how to initialize the pages. I have noticed that in most of the tutorials they have initialized the pages in constructor itself.
Example:
public LoginPage(WebDriver driver)
{
this.driver = driver;
PageFactory.initElements(driver, this);
}
Likewise in all the page classes, they have added initElements
method inside constructor itself.
But there are some sites where I noticed that instead of initializing all the pages in constructor, first page they initialized in constructor and for all other pages they initialized at return statement of some method (being executed at last in current page). If method "login" is the last method in LoginPage.java
, then it would initialize HomePage
as return type.
Example:
public HomePage login(String un, String pw)
{
...
...
return PageFactory. initElements(driver, HomePage.class);
}
My Doubt: Which one is the right way to implement and efficient one?