arena
arena

Reputation: 388

Where should the Page Objects be instantiated?

I would like to know where should I instantiate my Page objects? Here is my project hierarchy:

Pages : Contains all page objects with constructor such as

public LoginPage extends BasePage
 {
   super(driver);
   PageFactory.initElements(driver, this);
}

My BasePage contains all common methods such as table handling, data gathering from webtable etc..

I have a baseTest which contains all of the Page Objects instantiation and my tests are extend this class.

LoginPage loginPage = new LoginPage(driver);

I have helper (non-static) classes as well for navigation, database connection, custom waits etc.

Any best practice? Basetest is the proper place to instantiate them?

Thanks!

Upvotes: 0

Views: 1995

Answers (2)

Lew Bloch
Lew Bloch

Reputation: 3433

I instantiate page objects in the test code, but not the base class, as the scenario requires. E.g.,

@Test(dataProvider = PROVIDER)
public void testLogin(WebDriver driver, Info info) {
  Login login = new Login(driver);
  assertTrue(login.isDisplayed());
  login.enterCredentials(info.getUser(), info.getPw());
  Welcome welcome = new Welcome(driver);
  assertTrue(welcome.isDisplayed());
}

This is just one way to use page objects.

Be careful not to write too many "helper" classes, or too deep an inheritance hierarchy.

Upvotes: 1

Anish Pillai
Anish Pillai

Reputation: 1023

I do it in the way given below. I'm not sure how good it is. Comments and Feedback welcome.

a. I use an AbstractPage class which contains instantiates page objects, and also has all the object level wrapper methods such as clickButton(), sendKeys() etc. Page Object instantiation happens via constructor

public AbstractPage(WebDriver driverWeb) {
  this.driverWeb = driverWeb;
  PageFactory.initElements(driverWeb, this);
}

b. Then I have all my Page Classes which extend this AbstractPage -

public LoginCanvas(WebDriver driverWeb) {
  super(driverWeb);
}

c. Then a class which runs all my test cases. But I use cucumber so this base class just runs the cucumber scenarios.

Upvotes: 0

Related Questions