How to implement Page object model in selenium.

This post covers how to implement a page object model in selenium.

What is Page Object Model?

Page Object Model (POM) is widely used in a test automation framework that creates an Object Repository for web UI elements. The benefit of this model is, it reduces redundancy of code and helps in test maintenance.

Each page in the webpage will have a corresponding Page Class.

Naming conventions should be, for instance, if a page is waiting for a popup to disappear, it can be named as waitToClose().

Why do we require Page Object Model?

If we have used the objects and their actions in the same class and suppose if we had used the same element on different pages then all the pages need modification as shown below.

How to implement page object model in selenium

To avoid all those reworks, we can create a separate class file that has only find web elements, fill them or verify them. This class can be utilized in all the methods using that element. If there is a change in the element, we can change in just one location so that it will impact the changes at all occurrences.

Let’s create a Page Object Model

As an example, let’s create a page object for a page,

First, we will create a Page Object class for element Searchbar and SearchBtn as shown below

how to implement page object model in selenium.
package Demo;

import org.openqa.selenium.*;

public class PageObject {
	
	 private static WebElement element = null;
	
	// SearchBar
	   public static WebElement SearchBar(WebDriver driver) {
	      element = driver.findElement(By.xpath(".//*[@id = 'Search']//span"));
	      return element;
	   }
		
	   //Search Button 
	   public static WebElement SearchBtn(WebDriver driver) {
	      element = driver.findElement(By.xpath(".//*[@id = 'SearchIcon']/div//li//a"));
	      return element;
	   }

}
	

Now we can utilize the element in a test case, for that we need to call the methods created in PageObject Class by creating an object for the class as shown below.

how to implement page object model in selenium.
package Demo;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;


public class Test {
	private static WebDriver driver = null;
	  
	   public static void main(String[] args) {
		  
		   PageObject obj = new PageObject();
	    
	      driver = new FirefoxDriver();
	      driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
	      driver.get("http://www.google.com");
	  
	      //Use page Object library now
	      obj.SearchBar(driver).click();		
	      obj.SearchBtn(driver).click();
	
	

}
}

As shown above, we have utilized the objects created in PageObject class in our test cases, the same way, the objects can be utilized in many other test cases as well.

Conclusion

Page Object Model in Selenium WebDriver with TestNg is an Object Repository design pattern. Selenium page object model creates our testing code maintainable, reusable. We hope you can optimize your test pack by implementing the Page Object Model.

Leave a Reply