Page Object Model (POM) is one of the most common and popular design patterns used in automation testing. In the Page Object Model, each page in the web application is maintained as a separate class. All the actions that can be performed on that web page are defined as methods inside the class.
For example, if we are writing a script for the login page, a class will be created with the name LoginPage. Usually, in a login page, we have a username input, a password input, a login button, a forgot password button, and a sign-up link.
In the Page Object Model, inside the LoginPage class, a method is created for each user action. For example, to enter a username, a method will be created inside the LoginPage class.
Example Code for LoginPage.java
This enterUsername method enters the given text into the username field on the login page. This method is then called in the loginTest class. In the test class, we can pass different values to the method parameter to perform the action.
Example Code for LoginTests.java
Now you can see how the Page Object Model is useful.
The enterUsername method in the LoginPage class is used for both positive and negative test cases in the LoginTests class. We can pass a valid or invalid username through the method parameter. If the locator gets changed in the future, we only need to update it in one place, inside the LoginPage class.
Here are some of the major advantages of following the Page Object Model.
It avoids code duplication (one method for both positive and negative cases).
It enables easy maintenance (even if the locator changes, we need to update only one place).
It allows easy grouping in tests.
It improves readability.
It makes debugging faster.