Introduction Of Page Object Model In Selenium WebDriver: Advantages And disadvatages
In this post, we will see below topics:
- Why we need Page Object Model?
- What is Page Object Model and its advantages?
Why we need Page Object Model?
When you write a code, it must have below properties:
- Easy to understand.
- Easy modifications.
- Proper categorization.
- Anybody should be able to locate desired lines of code easily in project folders.
- No duplicate codes.
Working with Selenium Webdriver has two major parts:
- Locating web element/s.
- Performing action on web element.
In previous posts, We have written locators and actions on web elements in same class. Consider below scenarios now:
- Can you reuse same code? Answer is No. If you need to use same web element in other place, you need to locate it again. It is developing same code again and again. Your code is not reusable. It produces duplicate code. Waste of effort and time.
- Can you modify locator easily? Answer is No. Suppose you have used same locator in multiple script files. After some deployment, there is change in locator. You will be fed up by updating locator in all script files. Imagine if there are multiple locators are changed. Using POM, you just need to modify in one class and it will be updated automatically in all scripts.
- Can you or anyone can navigate to your code easily? Answer is No. Suppose, you/any other colleague of yours need to use same code again in another class. You know that you have written it already and want to copy lines of code from that old class. But finding old class name will be difficult as there will be many classes. After finding class, you need to find out line number of code. It is very difficult. Traversing your code is not easy.
- Can you easily distinguish similar web elements on multiple web pages? Answer is No.
- Can this code auto update reference of web elements when page is reloaded or refreshed. Answer is No. We can reuse already found locators until page is not refreshed./reloaded. For example:
Above code will give you staleElementException because reference of “ele” has changed. To know more about this refer this post.
All above scenarios can be handled easily by using Page object model in selenium webdriver.
What is Page Object Model and its advantages?
- Page Object Model(POM) is an object design pattern in Selenium webdriver which says how to organize your object repository. Object means web elements here.
- It is not a selenium framework.
- POM is used to create object repository in such a way that web elements can be added, reused and modified easily. This object repository has web elements name and locators to find it at same place.
- In POM, each web pages are represented as a separate class. For example, consider MakeSeleniumEasy website. It has many web pages like Home , Selenium Topics, Frequently Asked Java Programs etc. All these web pages contain different topics. I use different web pages to categories my topics. POM says same that we should have categorization of web elements according to web page in which they are present.
- If any new web elements are added in to a web page, You can add that new web element in object repository by navigating to class which has same name as webpage. For example: I add a new web element in Home page. To add new web element in object repository, you will search from Home.java file and will add in to it.
- If locator for any web element is changed, you just need to identify from which webpage it belongs. After that you need to navigate to particular webpage java file. find web element and modify its locators.
- POM also says that create all user possible interaction on web elements in same page as a method. For example: You are on Home page and it has web elements like “User Name” and “Password” text boxes. You can create methods as below:
public void enterUserName(String userName)
public void enterPassword(String password)
It will be very useful as any developer just need to call methods directly in stead of call locators first and then perform actions on it. It is called functional encapsulation.
- Using POM, you can achieve coordination between pages. Suppose after login, you are moved to another web page named Dashboard. Using logic page method you can return an object of Dashboard page. We will see it later.
- Object repository is made independent of any test scripts. Whatever objects you need to develop test scripts just call from that particular object repository.
- POM helps in handling staleElementException without writing any extra code by developers. POM reinitialize objects whenever it is called. It page gets refreshed or reloaded, it takes care of new reference of web element. We will see this later in detail.
- POM is lazy object locators. It will not find web elements until element is used. We will see it later.
- POM enhances test maintenance, readability and reducing code duplication.
- POM can be used with any type of selenium framework like Keyword, Data driver, hybrid etc.
- Using POM, you can achieve abstraction and encapsulation.
- Selenium webdriver provides a default implementation of Page Object Model using Page Factory which has more advantages than plain POM. We will see them later.
Disadvantages of Page Object Model:
- You need to be little good in coding.
- Developers may develop duplicate web elements if naming convention is not followed.
- If a web page contains multiple div or iframe, POM class becomes bigger and difficult to manage. You can break pages in to multiple sub pages.
- If webpage contains similar elements in different div/frame, you need to be careful for naming convention.
- POM is specific to project. It is not generic.
- You can get StaleElementException in case of findElements case. For example: Consider you are applying filter conditions and after checking each filter checkbox page gets refreshed. If you use findElements to store all checkbox and iterating it to select multiple filter conditions, it will give you StaleElementException for second iteration as page gets refreshed after first selection.
Is Page Object Model is a selenium framework?
No, its not a selenium framework. It is a design pattern. You can use POM in any type of framework.
We will see implementation of Page Object Model in upcoming posts. Stay tuned.
If you have any doubt, feel free to comment below.
If you like my posts, please like, comment, share and subscribe.
Author: Amod Mahajan
A software Tester who is paid to judge products developed by others. Currently getting paid in American Dollars. Writing technical posts and creating YouTube videos are my hobbies.
11 thoughts on “Introduction Of Page Object Model In Selenium WebDriver: Advantages And disadvatages”
Thanks for sharing your knowledge. It will be helpful for us.
Could you please how to pass driver object from home page to login page in POM
Will post soon.
U can use Constructor for same
Good one dude!!!