How to implement Page Object Model (POM) in Selenium?

Learn how to implement Page Object Model in Selenium

How to implement Page Object Model (POM) in Selenium?

What is Page Object Model?

It is a design pattern frequently used with test automation frameworks. It helps in reducing code duplication, increases reusability and reduces maintenance efforts. It contains different page classes which contains web elements and methods to interact with web elements. Each page class represents a page or feature in the application.

Example:

If an application has 3 pages – Login, Home and Register Page. Then we can create 3 different Page Object classes with all web elements and methods in each page as shown below:

Advantages of POM:
1. Code is much cleaner and optimized.

2. Complete abstraction of internal code from the tests.

3. Methods and objects can be reused across different tests.

4. No code duplication

5. Maintenance efforts are much lesser compared to other frameworks.

6. It can act as an Object Repository for Automation Framework.

How to implement Page Object Model in project?

Lets take an sample login test script written in Selenium Java which has the following steps:

– launches a web application

– enters username and password

– click on submit button

Implementation with POM:

1. Create a new class LoginPage.java with the following webelements and methods:

2. Modified Login test script

As we can see, our test script code is very optimized and abstracted from the underlying code. All the logic is written within the LoginPage class and the test script only contains calls to the reusable methods.

Every time there is any change to any web element identifier or methods, we need to only change it in the Page Object classes and our test script remains unaffected.

Bijan Patel
Full Stack Test Automation Expert | Selenium Framework Developer | Certified Tosca Automation Specialist | Postman | DevOps | AWS | IC Agile Certified | Trainer | Youtuber | Blogger|

Launch your GraphyLaunch your Graphy
100K+ creators trust Graphy to teach online
𝕏
QASCRIPT 2024 Privacy policy Terms of use Contact us Refund policy