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 = By.name(“username”);
By Password = By.name(“password”);
By LoginButton = By.id(“tdb1”);
By OnlineCatalogLink = By.linkText(“Online Catalog”);
By ErrorMessage = By.className(“messageStackError”);

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

public void clearUsername() {
pageClassdriver.findElement(Username).clear();
}

public void typePassword(String Pwd) {
pageClassdriver.findElement(Password).sendKeys(Pwd);
}

public void clearpassword() {
pageClassdriver.findElement(Password).clear();
}

public void ClickLogonButton() {
pageClassdriver.findElement(LoginButton).click();
}

public void clickLink () {
pageClassdriver.findElement(OnlineCatalogLink).click();
}

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 (https://gcreddy.com/project/admin/login.php)
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

Expected:
https://gcreddy.com/project/admin/index.php

Selenium Test Case:

public class Testcase1 {

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

//Create Admin Login Test Case
testCase1driver.get(“https://gcreddy.com/project/admin/login.php”);
login.typeUsername(“gcreddy”);
login.typePassword(“Temp@2020”);
login.ClickLogonButton();
String url = testCase1driver.getCurrentUrl();

if (url.equals(“https://gcreddy.com/project/admin/index.php”)) {
System.out.println(“Admin Login is Successful – Passed”);
}
else {
System.out.println(“Admin Login is Unsuccessful – Failed”);
}
testCase1driver.close();
}
}

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 (https://gcreddy.com/project/admin/login.php)
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.

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(“webdriver.chrome.driver”, “C:\\Users\\USER\\Downloads\\chromedriver.exe”);
WebDriver testCase2driver = new ChromeDriver();
testCase2driver.manage().window().maximize();

LoginPage eMessage = new LoginPage(testCase2driver);

//Create Error Message Test Case
testCase2driver.get(“https://gcreddy.com/project/admin/login.php”);
eMessage.typeUsername(“abcdxyz”);
eMessage.typePassword(“Temp@0000”);
eMessage.ClickLogonButton();

String errorMes = eMessage.captureErrorMessage();

if (errorMes.contains(“Error: Invalid administrator login attempt.”)) {
System.out.println(“Passed”);
}
else {
System.out.println(“Failed”);
}
testCase2driver.close();

}
}

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
(https://gcreddy.com/project/admin/login.php)
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:
https://gcreddy.com/project/

Selenium Test Case:

public class Testcase3 {

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

LoginPage redirect = new LoginPage(testCase3driver);

//Create Page Redirection Test Case
testCase3driver.get(“https://gcreddy.com/project/admin/login.php”);
redirect.typeUsername(“gcreddy”);
redirect.typePassword(“Temp@2020”);
redirect.ClickLogonButton();

String url = testCase3driver.getCurrentUrl();

if (url.contains(“https://gcreddy.com/project/admin/index.php”)) {
redirect.clickLink();
}
String url2 = testCase3driver.getCurrentUrl();

if (url2.equals(“https://gcreddy.com/project/”)) {
System.out.println(“Page is redirected to User Interface – Passed”);
}
else {
System.out.println(“Page is Not redirected to User Interface – Failed”);
}
testCase3driver.close();

}
}


Manual Testing Tutorial

Java Tutorial

Python Tutorial

SQL Tutorial

Selenium, Java, and Testing Videos

Follow me on social media:

1 thought on “Page Object Model in Selenium”

Leave a Comment