Page Object Model in Selenium

Price: INR 1,299.00
Was: INR 1,799.00

First, let’s understand, What is Page Object Model ?

In the previous tutorials, We have seen how can we generate the Selenium Test Scripts. Generate the Selenium Test is not the tough task.

In our given examples we used to keep the whole code in the single class but with time test suite will grow. As you add more and more lines to your code, things become tough.

 

We are used to keeping the Element locators, actions on these and Test methods in the same class. But this approach will make the script difficult to maintain. Suppose you are using single web Element in hundred of test and later there are some changes introduced in the Element location then you need to amend the same change at 100 places in your test case, Which is not a good practice. This is time-consuming and error-prone.

 

Page Object Model in Selenium

A better approach to script maintenance is to create a separate class file which would find web elements, fill them or verify them. This class can be reused in all the scripts using that element. In future, if there is the change in the web element, we need to make the change in just 1 class file and not 10 different scripts.

This approach is called Page Object Model(POM). It helps make the code more readable, maintainable, and reusable. The main advantage of Page Object Model is that if the UI changes for any page, it doesn’t require us to change any tests, we just need to change only the code within the page objects (Only at one place). Many other tools which are using selenium, are following the page object model.

 

Page Object Model is a design pattern to create Object Repository for UI elements. In POM  Model, for each web page in the application, there should be corresponding page classes.

 

These Page class will find the WebElements of that web page and also contains Page methods which perform operations on those WebElements. Name of these methods should be given as per the task they are performing.

 

This approach is called Page Object Model(POM). It helps make the code more readable, maintainable, and reusable. The main advantage of Page Object Model is that if the UI changes for any page, it doesn’t require us to change any tests, we just need to change only the code within the page objects (Only at one place). Many other tools which are using selenium, are following the page object model.

 

To create the Test, we create the separate classes for Tests on corresponding pages or functionality. We can understand this using below mentioned structure –

Page Object Model

 

In the above example, we can see, We placed the Page Classes in the Separate package and Test Classes in the separate package to make the code more maintainable, reusable and readable.

 

Advantage of POM

  • Page Object Model makes our code cleaner and easy to understand.

  • POM makes object repository is independent of test cases, so we can use the same object repository for a different purpose with different tools.

  • Code becomes less and optimized because of the reusable page methods in the POM classes.

 

0 Comment

Leave a Reply