Image
How to implement Page Object Model

How to implement Page Object Model

Rohan Natu
July 9, 2021

Page Object Model (POM) is a design pattern used in Test Automation that creates an Object repository for Web Elements. It is used for exalting test maintenance and reducing code duplication. A Page Object is an object-oriented class that provides an interface to a page. The automation test uses the methods of Page Object class when they need to interact with the UI of the page. This page class determines the Web Elements of the web page and contains page methods that operate on the Web Elements.

blog

Read more on Jade Global's automation solutions for testing mission-critical business processes across the enterprise that cut project delivery timelines, reduce manual effort and mitigate risk.

Advantages of Page Object Model

  1. POM helps keep our code clean, readable, and easy to understand. Also, it helps maintain our code in a more organized way.
  2. Page Object repository is independent of Automation Tests. The advantage of keeping a separate repository for Page Objects allows us to use this repository for different purposes with different frameworks. We can integrate this repository with other tools as well.
  3. It helps maintain our test cases in a short and optimized way, as we can reuse the Page Object methods in the POM classes.
  4. Any UI changes can easily be implemented, updated, and maintained into the Page Objects and Classes.
  5. It uses a single repository for the services or operations provided by the page, rather than having these services scattered throughout the tests.

How to Implement Page Object Model in your project

Here, we will try to run the given steps using a simple example of Page Object Model with Demo website

  1. Open the following website https://testyou.in and Go to the Login page using the Login link.
  2. Verify that the Login page contains Login Id and Password fields.
  3. Enter the Login Id and Password and click on the Login button.
  4. Close the Browser.

Note: Here, we are dealing with the Login page only. Accordingly, we create 1 POM in the selenium class.

Now, let us move to "How to Implement the above steps using POM in Selenium Web Driver."

  1. First, create a new directory/folder/package in your project depending upon which IDE you are using and name it as PageObject.
  2. Right-click on the folder and add a new Class file to it. Name it to the actual page from test object, i.e. pageObject -> LoginPageInfo. Blog
  3. The Login screen of the dummy website looks like the following. The highlighted element is the one that we are going to store in our LoginPageInfo.java class. Blog
  4. LoginPageInfo class will contain web elements present on the Login screen like the Login Id, Password, and Login button.
  5. After we click on Login from Home Page, we should be redirected to the Login Page. Here, we enter the username and password and then click on the Login button.
  6. Our LoginTest class will contain the web elements and the corresponding actions as given in the code below: Blog

    This is the POMRunner (Main) class from where we will call Page Objects.

    Blog
  7. After creating the required classes for the different web pages, you need to create a test class with execution steps. Steps in this class would refer to the object repository created for different page elements.

Conclusion

Page Object Model is a great way to create tests that are easy to maintain, and It allows you to scale up your testing efforts faster as you can reuse the object as many times as needed. Using the Page Object Model, all the element locators are maintained in a separate directory and can be updated easily without any change in the test cases. Any change in the UI can easily be executed, updated, and maintained into the Page Objects and Classes.

Want more on Test Automation? Read our Blog: 10 Benefits of Selenium Test Automation

About the Author

Rohan Natu, Software Engineer – QA

Rohan is a Software Engineer at Jade Global. He has 6+ years of experience in Software Testing in Agile and expertise in Functional and Automation Testing, Integration Testing, and ServiceNow application Testing.

Add New Comment

Restricted HTML

  • Allowed HTML tags: <a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd> <h2 id> <h3 id> <h4 id> <h5 id> <h6 id>
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.