0

I am practicing POM and Page Object factory. I would like to learn good practices in instantiating the pages.

till now I am Instantiating the pages from test class (e.g. TC_01_Register.java) using the below statement, I am not passing driver object to page class

OCHomePage ochome = PageFactory.initElements(wdriver,OCHomePage.class);

another way I was thinking is, creating the class object where Page class receives the constructor and instantiate the page factory in constructor it self

OCHomePage ochome = new OCHomePage(wdriver);

OCHomePage class (code snippet)

public OCHomePage {
    OCHomePage (WebDriver wdriver){
        PageFactory.initElements(wdriver,this.class)
   }
  @findby... // code continues ..
}

please suggest me if any other best ways available or the practices you follow..

0

Both options are fine, but you can improve it in two ways:

  • Abstract the creation to a setup method on the test class, assuming each class is related to each application page.
  • Abstract the factory initiation (and other common steps) to a base class:

    public OCHomePage extends BasePage {
        OCHomePage (WebDriver wdriver){
            super(wdriver);
        }
    }
    
    public BasePage {
        BasePage (WebDriver wdriver){
            PageFactory.initElements(wdriver,this.class);
        }
    }
    

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.