Page Object Model is a design pattern Concept which has become popular in test automation for enhancing test maintenance and reducing code duplication.

A page object is an object-oriented class that serves as an interface to a page of your AUT.

The tests then use the methods of this page object class whenever they need to interact with the UI of that page, the benefit is that if the UI changes for the page, the tests themselves don’t need to be changed, only the code within the page object needs to change.

Subsequently all changes to support that new UI are located in one place.

Need of POM

Increasing automation test coverage can result in unmaintainable project structure, if locators are not managed in right way. This can happen due to duplication of code or mainly due to duplicated usage of locators.

For Example, in home page of web application we have menu bar which leads to different modules with different features.

Many automation test cases would be clicking through these menu buttons to execute specific tests. Imagine that the UI is changed/revamped and menu buttons are relocated to different position in home page, this will result automation tests to fail.

Automated test cases will fail as scripts will not be able to find particular element-locators to perform action.

Now, QA Engineer need to walk through whole code to update locators where necessary. Updating element-locators in duplicated code will consume a lot of time to only adjust locators, while this time can be consumed to increase test coverage.

We can save this time by using Page Object Model in our test automation framework.

Page Object Class

Page Object class contains all the page object(web elements) for a particular page. For every page present application we have to write the Page Object class.

Page Object class name should end with PO, so that it will be easy for user to identify class.

  • Create a Java project in eclipse and add all selenium webdriver jars
  • Create package called po some people also prefer objectrepository without space
  • Create a Class called GooglePO, which is Page Object Class. This class will store all the web elements present in the Google Page
  • Once we have created the PO class, we have to add webelements to the class.

Library Files

Sometimes we may have a repeating test steps across script, in such cases we have to create those steps as re-usable methods. Re-usable methods can be used across multiple scripts in framework

Steps to create Library :
1. Library Name should be Same as PageObject Class name except the PO at the end, library file names will have Lib in end like HomePageLib, SearchResultPageLib...

2. Create a class called HomePageLib under lib package,

3. Import the static member of InitializeLogging class

4. We have to create create object for the PO class that we are going use in this Lib file. Eg:hp = new HomePagePO();

5. Write all the methods as non-static methods, and return the values if required.

6. We can add log statement based on our need

Complete HomePageLib


										import static base.InitializeLogging.*;
										import po.HomePagePO;

										public class HomePageLib {
											hp = new HomePagePO();
											
											public void searchGoogle(String searchTerm){
												hp.searchBar().sendKeys(searchTerm);
												log.info("Searching google for the term : "+searchTerm);
												hp.searchButton().click();
												log.info("Google search button clicked");
											}
										}

										
Note: For example purpose I have created only one method

Test Cases With Unittest in Selenium Python

We have create testcase class to create test scripts, which are nothing but automation scripts for our manual test cases. We are using TestNG tool for mapping manual and automation test cases. Every @Test denotes that it s a testcase, and result will be generated based on this test.

Steps to create test case Class :

1. Create a class called HomePageTest, name of the class should be accordance with PO and Lib classes

2. Import the static member of the InitializeBrowser class so that we can access browser variable.

3. Create a non-static method to write the test steps and annotate the method with @Test annotation

4. based on our need we can write @Before and @After annotations

5. Log details if required, details like what test case, test case name, test case number, what step is running

Complete file of HomePageTest


										import org.testng.annotations.AfterClass;
										import org.testng.annotations.BeforeClass;
										import org.testng.annotations.Test;
										import static base.IntializeBrowser.*;

										import lib.HomePageLib;

										public class HomePageTest {

											HomePageLib hpLib = new HomePageLib();
											@BeforeClass
											public void setUp(){
												browser.get("https://google.com");
											}
											@Test
											public void searchGoogleTest(){
												hpLib.searchGoogle("first test with POM");
											}
											@AfterClass
											public void tearDown(){
												browser.quit();
											}
										}

										

Advantages of Page Object Model

  • According to Page Object Model, we should keep our tests and element locators separately, this will keep code clean and easy to understand and maintain.
  • The Page Object approach makes test automation framework programmer friendly, more durable and comprehensive.
  • Another important advantage is our Page Object Repository is Independent of Automation Tests. Keeping separate repository for page objects helps us to use this repository for different purposes with different frameworks like, we are able to integrate this repository with other tools like JUnit/NUnit/PhpUnit as well as with TestNG/Cucumber/etc.
  • Test cases become short and optimized as we are able to reuse page object methods in the POM classes.
  • Any change in UI can easily be implemented, updated and maintained into the Page Objects and Classes

About Author

Myself KarthiQ, I am the author of this blog, I know ways to write a good article but some how I donot have the skills to make it to reach people, would you like help me to reach more people By sharing this Article in the social media.

Share this Article Facebook
You can also share knowledge by Adding a topic here


Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions

Recent Addition

new tutorial Selenium Online Training : Our next online training course for Selenium with Java starts from 17th December 2018.

You can attend first 3 classes for free, the total course fee is INR 10,000

The course time would be 8.00 PM(IST) for the first three classes

If you are interested to learn, then you can join the course by sending email to chercher.tech@gmail.com

or Register below


 
Join My Facebook Group
Join Group