Page Object Model in Selenium

Page Object Model in Selenium, Create Object Repositories in Selenium, Advantages of Page Object Model, and reuse elements in Selenium Test Cases.

Page Object is a Design Pattern that 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 test cases then use the methods of this page object class whenever they need to interact with the UI of that page.

Page Object Model is an Object/Element design pattern to create Object Repositories

We create object repositories for reusing objects/elements and centralized maintenance of elements/objects

Under this POM, container classes for web elements are created that behave as Object Repositories

We can create POM in two ways
1. POM without Page Factory
2. POM with Page Factory

Under this POM, for each web page there should be a corresponding page class, this page class finds the web elements of the web page and also contains customized commands which perform actions on web elements.

Implementing Page Object Model

In this approach, all pages of the application and user actions on web elements are maintained as methods, by importing the page class we can create test cases.

Create Page Class for Login Page:

(Using Username Edit box, Password edit box, Login Button, Online Catalog Link, and Error Message elements…)

public class LoginPage {
WebDriver pageClassdriver;

//Create Constructor
//A Constructor in Java is a block code similar to a method
//this keyword invokes an instance of the current class constructor that is called an instance created.
public LoginPage(WebDriver argumentdriver) {
this.pageClassdriver= argumentdriver;

//Create Elements information using Element Locators
By Username =“username”);
By Password =“password”);
By LoginButton =“tdb1”);
By OnlineCatalogLink = By.linkText(“Online Catalog”);
By ErrorMessage = By.className(“messageStackError”);

//Create Customized Commands
public void typeUsername (String Uname) {
public String captureUsername() {
String val =pageClassdriver.findElement(Username).getAttribute(“value”);
return val;

public void clearUsername() {

public void typePassword(String Pwd) {

public void clearpassword() {

public void ClickLogonButton() {

public void clickLink () {

public String captureErrorMessage() {
String message = pageClassdriver.findElement(ErrorMessage).getText();
return message;


Create Test Case Classes:

Test Case 1: Verify Admin Login Functionality in gcrShop application Admin interface.

Test Steps:

1. Launch a Browser
2. Navigate to gcrShop Admin Interface Home / Login page (
3. Enter valid “Username”
4. Enter valid “Password”
5. Click “Login” Button
6. Close Browser Window

Input/Test Data:
Username: gcreddy
Password: Temp@2020

Verification Point:
Capture the current URL after clicking on Login Button and Compare with expected


Selenium Test Case:

public class Testcase1 {

public static void main(String[] args) {
System.setProperty(“”, “C:\\Users\\USER\\Downloads\\chromedriver.exe”);
WebDriver testCase1driver= new ChromeDriver();
LoginPage login = new LoginPage(testCase1driver);

//Create Admin Login Test Case
String url = testCase1driver.getCurrentUrl();

if (url.equals(“”)) {
System.out.println(“Admin Login is Successful – Passed”);
else {
System.out.println(“Admin Login is Unsuccessful – Failed”);

Test Case 2: Verify “Error Message” in Login Functionality.

Test Steps:

1. Launch the Browser
2. Navigate to (Load) gcrshop admin interface login page /home page (
3. Enter Invalid Username
4. Enter Invalid Password
5. Click “Login” Button
6. Capture the Error Message
7. Close the Browser Window

Verification Point:
Capture the Error message and compare with expected.

Error: Invalid administrator login attempt.

Input / Test Data:
Username: Abcdxyz
Password: Temp@0000

Selenium Test Case:

public class Testcase2 {

public static void main(String[] args) {
System.setProperty(“”, “C:\\Users\\USER\\Downloads\\chromedriver.exe”);
WebDriver testCase2driver = new ChromeDriver();

LoginPage eMessage = new LoginPage(testCase2driver);

//Create Error Message Test Case

String errorMes = eMessage.captureErrorMessage();

if (errorMes.contains(“Error: Invalid administrator login attempt.”)) {
else {


Test Case 3: Verify Page Redirecting Functionality from Admin to User Interface in gcrShop application.

Test Steps:
1. Launch a Browser
2. Navigate to/Load/Open/Launch gcrShop application Admin Interface home page
4. Enter “Username”
5. Enter “Password”
6. Click “Login” Button
7. Click “Online Catalog” Link
8. Capture the Current Url
9. Close the Browser window

Input/Test Data:
Username: gcreddy
Password: Temp@2020

Verification Point:
Verify Page Redirecting functionality from Admin to User Interface after Admin Login by checking the User Interface URL

Expected URLs:

Selenium Test Case:

public class Testcase3 {

public static void main(String[] args) {
System.setProperty(“”, “C:\\Users\\USER\\Downloads\\chromedriver.exe”);
WebDriver testCase3driver = new ChromeDriver();

LoginPage redirect = new LoginPage(testCase3driver);

//Create Page Redirection Test Case

String url = testCase3driver.getCurrentUrl();

if (url.contains(“”)) {
String url2 = testCase3driver.getCurrentUrl();

if (url2.equals(“”)) {
System.out.println(“Page is redirected to User Interface – Passed”);
else {
System.out.println(“Page is Not redirected to User Interface – Failed”);


Manual Testing Tutorial

Java Tutorial

Python Tutorial

SQL Tutorial

Selenium, Java, and Testing Videos

Follow me on social media: