The Page Object Model or POM is a design pattern used in Selenium WebDriver that creates an object repository for storing all the web elements. It is very useful when minimizing the code and removing redundancies. POM also assists QA teams when enhancing the reusability and efficiency of code.
Advantages of the Page Object Model:
- Code Re-usability: POM increases code reusability. Teams can write common methods that can be used for different web pages. For example, if there is a calendar functionality in a web application on different web pages. So, we can create a common method to handle the calendar functionality and use the same for each page class.
- Code Readability: POM increases the code readability as each web page has its individual class file to store its Locators, Methods, and Test Steps.
- The Locator class file contains only related locators for the specific web page say, By.id, By.xpath, By.name, By.className, etc.
- The Method class file contains logic methods related to the specific web page.
- The Test class file contains related test steps for a specific web page.
For example, we have a login web page for which we can create 2 class files:
- LoginPage.java: contains locators and methods related to login web page
- LoginTest.java: contains test steps related to login web page
- Code Maintenance: Code maintenance is easy with POM. In case any updates are required, we can directly change the code at the method level (the common method) and updates will reflect everywhere.
- The test cases can become short and clean.
- We can implement POM with other tools like TestNG, JUnit.
Working of Page Object Model: