Where should the Page Objects be instantiated?

匆匆过客 提交于 2019-12-12 01:49:59

问题


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!


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/43727174/where-should-the-page-objects-be-instantiated

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!