Search code examples
javaseleniumselenium-webdriverpageobjectspage-factory

Should I instantiate objects in the Base Page - Selenium Java


I am writing a Java Selenium Test Automation Framework. I am using Page Object Model. I have a Base Page that contains elements and methods that can be used by all of the other Pages that extend the Base Page.

Up to now I did NOT instantiate the Base Page. I instantiated all of the other pages as shown below:

public xxxxxxxxPage() {
    PageFactory.initElements(driver, this);
}

I didn't instantiate the BasePage as I think all of the BasePage elements get instantiated by the other pages that extend it.

However, today I wanted to use a common method that I had put on the BasePage. The method wanted to use an element that was also on the BasePage. However, it couldn't find the element as the BasePage hadn't been instantiated.

So is it ok for me to add the following code to the BasePage or is it the wrong thing to do?

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

Solution

  • Using PageFactory.initElements(driver, this); in BasePage will initiate the elements in the derived classes as well, you can use it in the base class constructor and delete it from the derived page objects.