Page Object Model is a design pattern to create Object Repository for web UI elements. Under this model, for each web page in the application, there should be corresponding page class. This Page class will find the Web-Elements of that web page and also contains Page methods which perform operations on those Web-Elements.
Page Object Model, also known as POM, is a design pattern in Selenium which has gained more popularity in the market for test automation development for the maintenance of code, such as reusability, extensibility, and avoiding code duplication etc.
Page Object Model is used for creating Control properties or Object Repository for controls on a web-page. For each web-page which we want to automate there should be a separate class such as if we are performing the automation for the login page, we need to maintain all the login page control properties in the separated class file. If we consider a login page, there are controls available in a login page such as UserName, Password, Login, ForgetPassword etc.
Each control will have unique control properties such as Name, Control ID, TagName, XPath, ClassName, CssSelector etc. To get the control properties we need to inspect the element by clicking F12 or right the webpage and clicking on Inspect Element.
There we can hover over the control to find a control property for each control. If the control doesn't have a control property then we need to use XPath. In order to take XPath right click on the control's element which is in the Inspect Element window, then click on Copy-> Copy XPath.
If we want to automate a login page then we need to create a separate class as LoginObjectProperties and maintain all the login related control properties in LoginObjectProperties class.
Similarly, if we want to automate the registration page, we will have control under registration page like Enter Email, FirstName, LastName, City, ZipCode, Country etc. For registration page, we need to create a separate class called RegisterationObjectProperties and keep all the registration related control properties in RegisterationObjectProperties class.
The maintenance of a separate class for object properties or control properties is used for reducing the duplication of code as well as to reuse the code.
Advantages of POM
- Page Object Pattern says operations and flows in the UI should be separated from verification. This concept makes our code cleaner and easy to understand.
- The Second benefit is the object repository is independent of test cases, so we can use the same object repository for a different purpose with different tools. For example, we can integrate POM with TestNG/JUnit for functional Testing and at the same time with JBehave/Cucumber for acceptance testing.
- Code becomes less and optimized because of the reusable page methods in the POM classes.
- Methods get more realistic names which can be easily mapped with the operation happening in UI. i.e. if after clicking on the button we land on the home page, the method name will be like 'gotoHomePage()'.