Page Object Model In Selenium Without Using Page Factory

Page Object Model (POM) is a Design Pattern to Maintain Object Repository for Web Elements. This Tutorial Explains how to Implement POM in Selenium Without Page Factory:

The scripted automation project has to be designed in such a way that it is optimized and easier to understand. This is achieved using POM which follows certain guidelines to design a framework.

We will learn more about:

  • What is POM?
  • Advantages of using POM in a project
  • How to Create a Basic POM without using Page Factory Approach?

=> Visit Here For The Exclusive Selenium Training Tutorial Series.

Page Object Model In Selenium Without Using Page Factory


Page Object Model Without Page Factory

Page Object Model is a design pattern that is used to maintain Object repository for the Web Elements. Here, all the Web Elements and their corresponding methods are maintained in a separate class for each webpage. Hence, even a single change in the attribute of a WebElement will reflect across all the test cases wherever it is used. In this way, it enhances easier Object Repository maintenance.

Page Objects Model is best when used for applications that have multiple pages or states.

Here are the Video Tutorials:

Part I


Part II

Advantages Of POM

Given below are a few advantages of POM:

  • It is easier to maintain the code. Any User Interface changes will reflect wherever it is used in the class.
  • Robust and Makes code readable (Methods have more realistic names).
  • Makes the code reusable and reduces duplication of code (Object repository is independent of test cases).
  • The code becomes less and optimized.

Steps To Create A POM Without Page Factory Model

#1) Create a Java Class for every page in the application.

#2) In each Class, declare all the Web Elements as variable.

#3) Implement corresponding methods acting on the variables.

The design pattern can be structured using 2 layers/packages:

  • Page Layer will hold the pages of the application as individual Java Class. Each Class will have WebElements declared as variables and the actions that are performed as methods.
  • Test Layer will hold the test cases of the application and its verification part.

Let's take an example of a simple scenario:

  1. Open the URL of an application.
  2. Type the Email Address and password data.
  3. Click on the Login button.
  4. Verify successful login message on the Search Page.

Page Layer

Here we have 2 pages,

  • HomePage: The page opens when the URL is entered and this is where we enter the data for login.
  • SearchPage: Page that gets displayed after a successful login.

In the Page layer, each page in the Web Application is declared as a separate Java Class and its locators and actions are mentioned there.

Steps To Create POM With Real-Time Examples

#1) Create a Java Class for each page:

In this example, we will access 2 web pages, “Home” and “Search” pages. Hence, we will create 2 Java classes in the Page Layer (or in a package say, com.automation.pages).

Package Name : com.automation.pages
HomePage.java
SearchPage.java

#2) Create WebElements as variables:

We would be interacting with:

  • Email, Password, Login button field on the HomePage.
  • A successful message in the SearchPage.

So we will create WebElements as variables using ‘By’ Class.

For Example: If Email has xpath as //div[contains(@id, ‘EmailId’)], then its variable declaration is

//Locator for EmailId field

By EmailAddress = By.xpath(//div[contains(@id, ‘EmailId’)])

#3) Create methods for actions performed on WebElements:

Below actions are performed on WebElements:

  • Type action on EmailAddress field.
  • Type action on the Password field.
  • Click action on the Login button.

For Example, User-defined methods are created for each action on the WebElement as,

public void typeEmailId(String Id){
driver.findElement(EmailAddress).sendKeys(Id)
}

Here, the Id is passed as a parameter in the method, as the input will be sent by the user from the main test case.

Note: A constructor has to be created for each class in the Page Layer to get the driver instance from the Main class in the Test Layer.

We do not initiate the driver here, rather its instance is received from the Main Class when the object of the Page Layer class is created.

Two Java Classes are created for each page as shown below:

HomePage.java

//package

com.automation.pages;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;

public class HomePage {

	WebDriver driver;
		
	// Locator for Email Address
	By EmailAddress = By.xpath("//div[contains(@id,'Emaild')]");

	// Locator for Password field
	By Password= By.xpath("//div[contains(@id,'Password')]");

	// Locator for SignIn Button
	By SignInButton= By.xpath("//div[contains(@id,'SignInButton')]");

	
	// Method to type EmailId
	public void typeEmailId(String Id){
		driver.findElement(EmailAddress).sendKeys(Id)
	}

	// Method to type Password
	public void typePassword(String PasswordValue){
		driver.findElement(Password).sendKeys(PasswordValue)
	}

	// Method to click SignIn Button
	public void clickSignIn(){
		driver.findElement(SignInButton).click()
	}

	// Constructor
	// Gets called when object of this page is created in MainClass.java
	public HomePage(WebDriver driver)
	{
		// "this" keyword is used here to distinguish global and local variable "driver"
		//gets driver as parameter from MainClass.java and assigns to the driver instance in this class 

		this.driver=driver;
			
}

SearchPage.Java


//package
com.automation.pages;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;

public class SearchPage{

	WebDriver driver;
		
	// Locator for Success Message
	By SuccessMessage= By.xpath("//div[contains(@id,'Message')]");

	// Method that return True or False depending on whether the message is displayed

	public Boolean MessageDisplayed(){
		Boolean status = driver.findElement(SuccessMessage).isDisplayed();
		return status;
	}

	
	// Constructor
	// This constructor is invoked when object of this page is created in MainClass.java

	public SearchPage(WebDriver driver)
	{
		// "this" keyword is used here to distinguish global and local variable "driver"
		//gets driver as parameter from MainClass.java and assigns to the driver instance in this class 

		this.driver=driver;

Test Layer

Test Cases are implemented in this class.

We create a separate package say, com.automation.test and then create a Java Class here (MainClass.java).

Steps To Create Test Cases:

  1. Initialize the driver and open the application.
  2. Create an object of the PageLayer Class(for each webpage) and pass the driver instance as a parameter.
  3. Using the object created, make a call to the methods in the PageLayer Class(for each webpage) to perform actions/verification.
  4. Repeat step 3 until all the actions are performed and then close the driver.
//package
com.automation.test;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class MainClass {

public static void main(String[] args) {
		
	System.setProperty("webdriver.chrome.driver","./exefiles/chromedriver.exe");
	WebDriver driver= new ChromeDriver();
	driver.manage().window().maximize();
	
driver.get("URL mentioned here");
		
// Creating object of HomePage and driver instance is passed as parameter to constructor of Homepage.Java
		HomePage homePage= new HomePage(driver);
		
// Type EmailAddress
	homePage.typeEmailId("abc@ymail.com"); 
// EmailId value is passed as paramter which in turn will be assigned to the method in HomePage.Java

// Type Password Value
	homePage.typePassword("password123"); 
// Password value is passed as paramter which in turn will be assigned to the method in HomePage.Java

// Click on SignIn Button
	homePage.clickSignIn();
		
// Creating an object of LoginPage and driver instance is passed as parameter to constructor of SearchPage.Java
	SearchPage searchPage= new SearchPage(driver);
		
//Verify that Success Message is displayed
	Assert.assertTrue(searchPage.MessageDisplayed());
		
//Quit browser
driver.quit();
		
}
}

Conclusion

This tutorial explained the advantages of the Page Object Model and how to create a basic POM design pattern without using Page Factory in Selenium.

In the upcoming tutorial, we will discuss yet another approach of POM, that is, using the Page Factory approach.

=> Check ALL Selenium Tutorials Here.