Creating Object Repository in Selenium WebDriver using Properties file

In this tutorial, we will know about the object repository in Selenium WebDriver.

Before understanding object repository, first, we understand what is the meaning of object with respect to Selenium?

What is Object with respect to Selenium?

An object is a graphics user element in a web application. For example, a button, edit box, image, link, etc. An object basically consists of Data and Functions.

Data within an object are known as properties of the object. Functions within an object are known as methods that can be performed on an object. This concept is nothing but object-oriented programming OOPs concept.

Let’s understand it with a realtime example.

Consider a fan as an example of an object. The data (properties) of a fan can be color, current speed, weight, etc. The functions of fan can be switching on-off of fan, changing speed of fan, etc.

Similarly, consider a button on a web page. A web button is an object in AUT. The properties or data of button can be name, width, height, visible, etc whereas, the functions or methods of a button can be clicking on the web button.

Hope that you understand the exact meaning of object w.r.t. Selenium. Let’s move to the main topic.

What is Object Repository?

A repository means a central location in which data is stored and managed. An object repository is a common storage location where we store a collection of all required application objects and their properties.

In other words, object repository is a central location where data is stored and managed. In context of Selenium WebDriver, object repository is mainly used to store element locator values in a central location to avoid hard coding within scripts.

Advantage of Object Repository in Selenium

The main advantage of using object repository in selenium automation is the separation of objects from test cases.

If the locator value of one web element changes, only the object repository needs to be changed rather than making changes in all test cases in which the locator has been used. This will increases the modularity of framework implementation.

Let’s understand the importance of Object repository with a realtime scenario.

The scenario is that suppose you have 100 test cases and in all test cases, have a login scenario.

If your application changes in the future and the locator value of one web element is also changed from email to login-email, you have to modify all the test cases and you have to change locator value email to login-email in all 100 test cases.

This task is very tedious and boring, which is also not possible. So, to overcome this, we will have to move all locators in a separate file and link all test cases to this file.

In the future, any changes happen in our locators, we will simply change in this file, not all our test cases.

Types of creating Object Repository in Selenium WebDriver

In QTP, when a user records a test, application objects, and their properties are stored by default in an object repository. QTP uses this object repository to play back scripts.

So, there is no need to create an object repository in QTP. It provides an in-built object repository by default.

But in the case of Selenium WebDriver, it does not provide an in-built object repository by default.

Object repository in Selenium can be created using the key-value pair technique wherein key refers to a name of an object and value refers to the property of an object that is used to uniquely identify an object within the web page.

There are two types of object repositories that can be created in Selenium WebDriver.

  1. Object Repository using Properties file
  2. Object Repository using XML file

Properties file in Selenium is a text file wherein data is stored in the form of key-value pairs. We will create properties file to store all element locators. This properties file acts as an object repository in Selenium WebDriver.

There are the following steps to create object repository using properties file in eclipse. They are:

Step 1: Creating Properties file in Eclipse

a) Create a java project named “ObjectRepositoryDemo” and then create a package named “com.objectRepository.demo” as shown in the below screenshot.

b) Now, right-click on the main project folder and select New -> Other.

Creating Object Repository in Selenium WebDriver using Properties file

c) In the next dialog window, select General -> File and then click on ‘Next’ button.

Creating Object Repository using Properties file

d) Name a valid file with the extension ‘.properties’ on the new file resource window and click on ‘Finish’ button.

Creating Properties file in Eclipse

e) A file named ‘’ will be seen on your Project Structure.

Folder structure of Object repository in Selenium

Step 2: Storing data into the properties file

a) In the properties file, data is stored in the form of Key-Value pairs. The key-value pair is represented by two string values separated by the equal sign.

b) We will store element name and locator values of WebElement into the properties file to identify web elements.

c) Let’s open the file in Eclipse and store the following sample data.

Storing data into properties file

Step 3: Reading data from Object repository properties file.

Once the properties file has been created, the next task is to read data from properties file that can be done using the built-in Properties class provided in java.util package in three simple steps.

a) First, create an object of the File class and pass the properties file path to its constructor.

File file = new File(path of properties file);

b) Second, we need to create an object of FileInputStream class with passing reference variable “file” of File class object as a parameter to its constructor.

FileInputStream fis = new FileInputStream(file);

c) Create an object of Properties class and pass reference variable “fis” of FileInputStream class object as a parameter to its constructor.

Now, call load() method provided by Properties class in java to read data from the properties file.

The following complete code is as follows:

Properties obj = new Properties();

How to use Object Repository (Properties file) in Test scripts?

Object repository can be used in test scripts by reading data from a properties file and passing the data using the getProperty() method of Properties class as a parameter to the findElement method.

The following code will be implemented to read data from the properties file in test scripts:

driver.findElement( By.xpath(obj.getProperty("Username"))).sendKeys("[email protected]"); 

In the preceding code, getProperty is a method that has been used to search for the property with the specified key in the property list. If the key is not found in the property list, it will return a null value.

The syntax of getProperty() method is as follows:

            public String getProperty(String key)

This method accepts the keys of String type and returns the specified key value in the property list.

Let’s take a test scenario where we will open the login page of Linkedin and send username and password to the text box by using properties file. We will use the properties file as shown in the above screenshot.

The complete code for the above test scenario is as follows:

Program source code:

package com.objectRepository.demo; 
import java.util.Properties; 
import java.util.concurrent.TimeUnit; 
import org.openqa.selenium.By; 
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.firefox.FirefoxDriver; 

public class DemoOR { 
public static void main(String[] args) throws IOException 
// Create WebDriver object. 
   WebDriver driver = new FirefoxDriver(); 
   String URL = ""; 
   driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); 

// Store properties file path in a variable named "path" whose return type is String. 
   String path = "C:\selenium\ObjectRepositoryDemo\src\com\objectRepository\demo\"; 

// Create an object of File class and pass path as a parameter to its constructor. 
   File file = new File(path); 

// Create an object of FileInputStream class and pass file as a parameter to its constructor. 
   FileInputStream fis = new FileInputStream(file); 

// Create an object of Properties class. 
   Properties obj = new Properties(); 

// Now call load() method of Properties class and pass fis as a parameter. 

// Locate web elements using By.xpath and 
// To locate web elements, we will pass keys specified in object repository to getPropertry() method. 
   driver.findElement( By.xpath(obj.getProperty("Username"))).sendKeys("[email protected]"); 
   driver.findElement(By.xpath(obj.getProperty("Signin"))).click(); driver.close(); 

In the above code, getProperty() method accepts keys specified in the object repository and returns the value specified with keys to the findElement() method to locate web elements.

Hope that this tutorial has covered almost all important points related to creating object repository in Selenium WebDriver with example scenario. I hope that you will have understood the properties file in selenium.
Thanks for reading!!!

⇐ Prev Next ⇒

Leave a Comment