Page Factory in Selenium POM Framework

In this tutorial, We will learn about the Page Factory in Selenium.

We will see how can we use the Page Factory Concept with Page Object Model in Framework designing.


What is Page Factory?

The PageFactory Class is an extension of the Page Object design pattern. It is used to initialize the elements of the Page Object or instantiate the Page Objects itself.Annotations for elements can also be created as the describing properties may not always be descriptive enough to tell one object from the other.


PageFactory is used to initialize elements of a Page class without having to use ‘FindElement’ or ‘FindElements’. Annotations can be used to supply descriptive names of target objects to improve code readability.


Use PageFactory in Framework Designing

In Page Object Model, First, we will design the Business Logic classes.

First, we will design the Page Classes. In PageFactory, we use the @FindBy annotations to store the WebElements.  i.e


public WebElement elementName;

i.e @FindBy(id=”username”)

public WebElement userName;


In Page Factory, we use the Locators like this to store the WebElements. After this, we perform the operations on these elements. Like in the below example we are inserting the text in above mentioned WebElement userName.

public void doLogin(String uName)





We need to learn more things in PageFactory like Page switching, Page Switching means when Selenium script will switch from one page to another page, How system or Framework will manage this. We will learn about this after this page class example. We are taking the landing page example to learn the PageFactory in Selenium.


In this Page class, First we have created the constructor of the class and invoked the WebDriver object.


Now, we are finding the Username and Password WebElements using @FindByannotations.

Now time to perform the action on these web elements. In the above code, we can see, We have only one method doLogin() which return type is HomePage . Now question is, What is this HomePage ?


HomePage is another class in Pages Package, this page class will keep the elements and methods of facebook Homepage i.e Page comes after login. In this method, we are returning the object of HomePage class.


We are doing this to support the Page Switching in Selenium. We are returning the HomePage object because after login operation Script will switch on HomePage and script will try to communicate with the HomePage WebElements. If we will not return this as Object then Selenium will not be able to communicate with the Homepage web events and our Testing script will stop.


Now let’s perform a Simple test –

Login to your facebook account and render to user Profile. Let’s see how can we do this. First, See the Framework Structure.

Page Factory in Selenium


In the above project structure, We have 4 packages. Let’s focus on com.seleniumbix.Pages and com.seleniumbix.TestCases . In Pages package, we have three different page classes, LoginPage which is facebook landing page, HomePage which page comes after login and ProfilePage this is user profile page.


Now comes to TestCases package. In this package, we have only one class . Now let’s see the code of all classes and understand, that how can we execute it.

We have already seen the LoginPage.Java class code.

Now let’s understand the above code we have already discussed the class.


In class we have single locator profileLink, In which we are using CSS Selector to find the element (Here Element ID is Dynamic, You can learn here How can we handle the Dynamic ID Elements ).


In this class, we have single method clickProfileLink(), this will click the Profile link and render the user on UserProfile page that’s why the return type of this method isProfilePage . Which is a blank class?


Now let’s discuss the LoginPageTest.Java class . In this method First we are creating the Object of LoginPage then we are using the object of LoginPage to create the Object of HomePage because doLogin() is returning the method of HomePage.


You can copy paste the above code and try on your local machine. In the case of any assistance please write us on our email or comment below.

0 Comment

Leave a Reply