Page Object Model (POM) & Page Factory in Selenium WebDriver

In this tutorial, we will learn how to use the Page Object Model and Page Factory in Selenium Webdriver with simple examples so that you can understand the things easily and implement them in your project works.

What is the Page Object Model (POM) in Selenium?

Page Object Model (POM) is a Design Pattern for automating web applications and it has become popular because it let us allow to create an Object Repository for Web Elements that translates into enhancing test maintenance and reducing code duplication.

As per the approach of this model, we have to create a Page Class for each web page in the application. This Page class is further used to identify the Web Elements of that particular page and also contains corresponding methods to perform required operations on those Web Elements.

Also, following the best practices, the name of these methods should be given according to the functionality of those web elements. For example, if there is a login button, the corresponding method could be like clickLoginButton() and such method names are self-explanatory.

The following is an example of a sample Page Object Model approach. Here I have created a package with the name “apppages” that contains all page classes of the application and a separate “tests” package that will contain all the tests for automating the web application. The project structure of the POM project looks like the following image.

Page object model pom in Selenium webdriver

Why Do We Need a Page Object Model In Selenium?

Automating a web application in Selenium WebDriver is quite an easier thing. We simply find elements and perform required operations on them.

Let’s take an example of automating http://www.advantageonlineshopping.com/. In this test script, we will open the “Advantage Shopping” application.Log into the application and verify the logged in user name on the Home page. The simple script without a Page Object Model will be like the following test.

The above script is quite simple. We are locating the desired elements and performing the required operations on them. If any of the project properties change in the future we can easily maintain this script. So far it looks good and there is no maintenance issue.

But in a real automation project, we have to write hundreds or thousands of automation scripts. You can just imagine if we follow the above simple approach and a few of the object properties got changed in the future due to any update in the application and those objects are being used in hundreds of tests, you will have to maintain on all impacted tests. In that case, maintenance will require humungous efforts. Also, it will be a very time-consuming task.

So how to tackle this problem. The better approach is to create a separate class file for each page that would find web elements on the page and perform all required operations on them like setting the value in a textbox and clicking a button and etc. This page class can be reused in all the scripts that are using their web elements.

Now in the future, if there is a change in the property of the web element, we just need to make the change in just one class file and not in hundreds of impacted scripts.

That is why this approach is called Page Object Model in Selenium. This approach makes the code more readable, maintainable, reusable, and scalable.

non pom vs pom based structure selenium

Advantages of Page Object Model in Selenium

  • Maintainable Code – The major benefit of the Page Object Model is that if the GUI changes for any of the pages, the tests themselves don’t need to change, only the code within the page object needs to change. This makes automation scripts easier to maintain and highly optimized.
  • Readable Code – Automation Test Scripts and Page-specific code are cleanly separated from each other. The code becomes less and more readable.
  • Reusable Code – If multiple test scripts have to use the same web elements, there is no need to write code to handle the web element in the individual test scripts. Having a separate page class makes it reusable by making it accessible by any test script.
  • Logical Method Name – The approach enforces us to give self-explanatory method names for page object elements that can be easily mapped with the operation to be performed on the GUI. i.e. if a have to click on a Login button, the method name could be clickOnLoginButton().

How to Implement the Page Object Model In Selenium?

In the section Why Do We Need a Page Object Model In Selenium? we have seen the simple automation script using without page object model structure.

In this section, we will automate the same test using the Page Object model.

Define Page Classes in POM in Selenium

In order to follow the Page Object Model, we will store the web elements present on each page in the corresponding java class. In this example, we will be interacting with two web pages that are Home Page and the Login page.

Home Page objects:

The Menu bar of the Home Page of the Advantage Shopping app looks like the following image. In this example, we will interact with two objects on the home page as highlighted in the below image.

Home page objects selenium

Note: Logged in user’s name object will be visible only after successful login

We will define only the above two objects in the page class of the landing home page as shown below. We will also define the corresponding methods that can be performed on the page object methods in the page class.

Home page class objects in selenium

The code for the LandingHomePage.java class is as follows where the page objects have been defined along with the methods to perform the desired action on the page objects.

Login Page Objects:

The login popup page looks like the following image.

login objects selenium

On the login Popup page, we will interact with only two objects that are user name and password. Instead of clicking on the Sign-in button we will press Enter key.

pom login objects selenium webdriver

The code for the LoginPage.java class is as follows:

How to Use Page Objects and Their Methods in Automation Test Classes?

The following VerifyLoggedInUser.java class uses both page object classes. In this class, I have created a POM-based script having two tests to give you a better idea of how we can implement page classes in multiple tests.

OutPut

If you run the tests the output will be as follows.

POM Selenium Result

What is Page Factory in Selenium & How to Implement it?

While understanding the Page Object Model we saw that it is a Design Pattern for automating web applications and it allows to create an Object Repository for Web Elements that translates into enhancing test maintenance and reducing code duplication.

Having said that, Page Factory in Selenium is an inbuilt class that supports the Page Object Model out of the box and way more optimized than a simple Page Object Model. In the Page Factory model, @FindBy annotation is used to find the elements, and the initElements() method is used to load the elements.

The following is an example of declaring an element using @FindBy annotation in Page Factory.

page factory in selenium

Note: We can use any of the locators in @FindBy annotation.

We will have to import “org.openqa.selenium.support.FindBy;” package to use @FindBy annotation.

The project structure of the Page Object Model and Page Factory will be the same as shown below and you don’t need to do any changes in the actual automation tests.

page factory structure in selenium webdriver

I have simply made a replica of project “POMExampleSelenium” to create “PageFactoryUsingPOMInSelenium” project and have done changes only on the page classes i.e LandingHomePage.java and LoginPage.java.

LandingHomePage with Page Factory

Login Page with Page Factory

VerifyLoggedInUser Test with Page Factory concept

This logical code of this class is the same as the Test class used in the Page Object Model. The only difference is the page class package name which I have changed here to apppagesfactory.

Test Result

Page factory in Selenium test result

AjaxElementLocatorFactory in Factory Class

AjaxElementLocatorFactory is a lazy load concept in Page Factory. It is used to identify web elements only when they are used in any operation. We can assign the timeout of a web element to the object class with the help of the AjaxElementLocatorFactory.The syntax is as follows.

Conclusion

In this article, we have seen how to use the Page Object Model and Page Factory in Selenium to make the automation code maintainable, reusable, readable, and highly optimizable. Hope you have liked this article.

Recommended Posts

Leave a Reply