How to implement POM in Selenium Java
· What is POM: POM or Page Object Model is a design pattern
· It is used to create separate object repository to store object locators
· In POM for each webpage there should be a separate page class
· The page class will have all object locators and preferably action methods for the corresponding page
In a normal class we include all the objects and locators along with test functions in a single class. However, in Page object model all the object locators are stored separately in a separate class and then the test methods are stored separately.

Let us implement POM in a simple and small Google search Test case.
We will start with creating a class for each web page.
We will start with a small test for google search. We have to open Google search page and type keywords to be searched in Google Search bar and hit Google Search button. The search results should appear in the google page.
We will create a separate class where we will store locators for the Google Search text box and also for the Google search button. We will create a new package where all the pages are stored separately.
Right click on src/test/java where we will select New Package and create a package. I have named the package as Pages. We will create a new class inside the Pages package. I have given the name of the class as Googlesearchpage.
Inside the Googlesearchpage class we will have functions for all the locators.
package Pages;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
public class Googlesearchpage {
private static WebElement element = null;
public static WebElement textbox_serach(WebDriver driver) {
element = driver.findElement(By.name(“q”));
return element;
}
public static WebElement button_serach(WebDriver driver) {
element = driver.findElement(By.name(“btnK”));
return element;
}
}
Now we will create another Package in src/test/java for the tests. I have named the package as Test.
We will create a new class inside the Test package. I have named the class as GooglesearchTest. The first thing we will do in our GoogleserachTest class is we will import the pages class.
For that we will import:
import Pages.Googleserachpage;
In this class we will write code for the tests to be run. That is, we will send keys to the Google Search text box and then we will click the google search button.
Our code for GooglesearchTest will look like this.
package test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import Pages.Googleserachpage;
public class GoogleserachTest {
private static WebDriver driver = null;
@Test
public void f() throws InterruptedException {
driver.navigate().to(“https://www.google.com/");
Thread.sleep(2000);
driver.manage().window().maximize();
Thread.sleep(2000);
Googleserachpage.textbox_serach(driver).sendKeys(“automation”);
Thread.sleep(3000);
Googleserachpage.button_serach(driver).click();
Thread.sleep(3000);
}
@BeforeTest
public void beforeTest() {
System.setProperty(“webdriver.chrome.driver”, “C:\\Program Files\\selenium\\chromedriver.exe”);
driver = new ChromeDriver();
}
@AfterTest
public void afterTest() {
driver.close();
}
}
Some of the benefits of using POM
· Test objects and functions are separated for easy maintenance and keeping the code clean.
· Objects are kept separated from test scripts and can be used multiple times in multiple tests.
· Every unique object locator is created only once.
· Easily maintained and less rework.
· Here POM was implemented for a very small test. We can implement it for larger tests with multiple locators.