Page Object Model in Selenium With Page Factory

Page Object Model in Selenium With Page Factory

Last updated on 21st Sep 2020, Artciles, Blog

About author

Ranjith (Sr Technical Project Manager )

He is a TOP-Rated Domain Expert with 6+ Years Of Experience, Also He is a Respective Technical Recruiter for Past 3 Years & Share's this Informative Articles For Freshers

(5.0) | 13747 Ratings 581
  • Page Object Model, also known as POM is a design pattern in Selenium that creates an object repository for storing all web elements. It is useful in reducing code duplication and improves test case maintenance.
  • In Page Object Model, consider each web page of an application as a class file. Each class file will contain only corresponding web page elements. Using these elements, testers can perform operations on the website under test.

Page Factory in Selenium

  • Page factory is the way to implement page object models. It is an inbuilt concept for the Page object model but very optimized. 
  • One of the biggest benefits of Page Factory is AjaxElementLocatorFactory Class. It works on lazy loading concept, i.e. a timeout for web elements is assigned to the page object class with the help of AjaxElementLocatorFactory. Hence, when an operation is performed on an element, the wait for its visibility starts from that particular moment only. If the element is not found in the given time frame, then the test case execution will throw an exception.
Subscribe For Free Demo

Error: Contact form not found.

Creating a page object model with page factory in Selenium WebDriver

Here, we will consider one Gmail application to learn the implementation of the page object model with a page factory in Selenium WebDriver.

Let us look at the steps to be followed for implementing the page object model using the page factory concept.

Step 1:Create a test base class. Here, we implement waits, create an object of WebDriver, maximize bowser, launch URL, etc. in this example, we will focus on chrome browser and set the system property to launch the Chrome browser. 

The programming code for the class is given below.


Step 2 :Now create classes for Facebook Login Page, Facebook Home page to hold web elements and locators. Usually, we should create page objects for all pages in AUT. 

For every page, we create a separate class with a constructor. Now, you should identify locators and store them in one class. It increases the reusability of locators for multiple methods whenever necessary. For any code changes in the UI, it makes the code maintenance easy and making changes to a particular page is simple. Here you should create Java files for both pages after the test base class to hold locators and their elements.

Step 3: As per the scenario, the script should work as follows:

  • Launch the Chrome browser and open
  • Enter the valid credentials and login to the Facebook home page.
  • Verify the credentials and log out.

In the end, you need to create one testing.xml file. The page object model always helps you to develop faster with cleaner code tests.

Advantages of Page Object Model

  • Helps with easy maintenance: POM is useful when there is a change in a UI element or there is a change in an action. An example would be if a drop down menu is changed to a radio button.

In this case, POM helps to identify the page or screen to be modified. As every screen will have different java files, this identification is necessary to make the required changes in the right files. This makes test cases easy to maintain and reduces errors.

  • Helps with reusing code: As already discussed, all screens are independent. By using POM, one can use the test code for any one screen, and reuse it in another test case. There is no need to rewrite code, thus saving time and effort.
  • Readability and Reliability of scripts: When all screens have independent java files, one can easily identify actions that will be performed on a particular screen by navigating through the java file. If a change must be made to a certain section of code, it can be efficiently done without affecting other files.
selenium Sample Resumes! Download & Edit, Get Noticed by Top Employers! Download

Difference Between Page Object Model and Page Factory in Selenium

Page Object ModelPage Factory
Finding web elements using ByFinding web elements using @FindBy
POM does not provide lazy initializationPage Factory does provide lazy initialization
Page Object Model is a design patternPageFactory is a class which provides implementation of Page Object Model design pattern
In POM, one needs to initialize every page object individuallyIn PageFactory, all page objects are initialized by using the initElements() method

Run the code in order to test the workings of Page Object Model and Page Factory. Since these are important Selenium functions, testers need to be able to use them with ease and accuracy for Selenium automation. This will help them streamline automation testing efforts and yield results quicker.

Are you looking training with Right Jobs?

Contact Us

Popular Courses