Page Object Model using Selenium Webdriver and Implementation

Page Object Model using Selenium Webdriver we are going to discuss a very important topic in Selenium, which you have to use in your project now or in future.

I get so many questions regarding Page Object Model framework but let me make it clear that Page Object model is just a design pattern, not a framework.It is not a new concept this concept is used by the programmer to make code robust and make code clean. We will also use Page Object model in Selenium to make our framework efficient.

This is one the most important questions in interviews as well because now a day every company is trying to switch from traditional object repository to POM.

 

Page Object Model using Selenium Webdriver.

 

What is Page Object model using Selenium Webdriver?

1- It is design pattern in which will help you to maintain the code and code duplication, which is a crucial thing in Test automation.

2- You can store all locators and respective methods in the separate class and Call them from the test in which you have to use.So the benefit from this will be if any changes in Page then you do not have to modify the test simply modify the respective page and that all.

3- You can create a layer between your test script and application page, which you have to automate.

4- In other words, it will behave as Object repository where all locators are saved.

“Please don’t get confused if you are using Object repository concept then do not use Page Object model because both will serve the same purpose.”

 

Implementation of Page Object model using Selenium Webdriver

If you want to implement Page Object model then you have two choices and you can use any of it.

1 – Page Object model without PageFactory

2- Page Object Model with Pagefactory.

Personally, I am a big fan of Page Factory, which comes with Cache Lookup feature, which allows me to store frequently used locators in catch so that performance will be faster. We will discuss both you can implement based on your preferences.

Page Object Model using Selenium Webdriver without PageFactory.

Let’s take very basic scenario which you can relate to any application. Consider you have login page where username, password, and login button is present.

I will create a separate Login Page, will store three locators, and will create methods to access them. Kindly refer below screenshot for reference.

Now I want to design test case so I can use the Login class, which I created and can call the methods accordingly.

This Login class will be used by all the scripts, which you will create in future so if any changes happened then you have to update only Login Class not all the test cases. This sounds good right.

 

I have created Youtube Video for the same

 

Program for Page Object Model using Selenium Webdriver without Page factory

 

TestCase using Page Object Model using Selenium Webdriver

 

Page Object Model using Selenium Webdriver with Page Factory

Selenium has built in class called PageFactory, which they mainly created for Page Object purpose, which allows you to store elements in cache lookup.

The only difference, which you will get without PageFactory and with PageFactory, is just initElement statement.Let us check the code and will see what changes required for with PageFactory Approach.

Another YouTube video for Page Object model with Page Factory

 

Code for Page Object Model Using Selenium Webdriver using Page Factory

 

Hope you have enjoyed the article. Feel free to comment below for any feedback.

32 Comments

  1. nisha b
  2. Anusha
  3. Anusha
  4. John
  5. skay
  6. praneeth
  7. Sravan K Kumar
  8. nidhi