3

I am practising 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 itself

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..

1
  • A third method is to add a factory method to the page object, that accepts the webDriver, creates the page object and initialises its elements. When a page navigates using a click it can use the factory method. Jul 14, 2019 at 14:36

1 Answer 1

1

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 agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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