Creating Object Repository in Selenium | BrowserStack (2024)

Home Guide How to Create Object Repository in Selenium

By Jash Unadkat, Technical Content Writer at BrowserStack - February 5, 2023

To test web apps from end-users’ perspective, QAs need to interact with multiple web elements. To do so, they use the concept of locators in Selenium while writing test automation scripts. However, hard-coding the locators in the script each time results in code duplication. Also if a value of any single locator changes, it needs to be updated throughout the project to ensure consistency. This is a time-consuming task.

Using object repository in Selenium helps QAs address both these issues. This article will explain the concept of object repository.

Table of Contents

  • What is an Object Repository in Selenium?
  • 1. How to create a Properties file and add data
  • 2. How to read data from the Properties file
  • 3. Using the Properties file in Selenium Script

What is an Object Repository in Selenium?

An object repository is a centralized storage of locators in the form of objects. QAs store all the element locators in a separate file, also referred to as a property file (.properties) in Selenium. The property file stores information in a key-value pair format. This file serves as an object repository in Selenium WebDriver. QAs can also store these objects in XML files depending on the framework being used.

The image below represents a sample Properties file. It has the extension named “.properties”

Creating Object Repository in Selenium | BrowserStack (1)
QAs can create two types of object repositories in Selenium WebDriver:

  1. Using a properties file in Selenium
  2. Using an XML file

Further, let us understand three things:

  1. How to create and store data in the Properties file
  2. How to read data from the Properties file
  3. Using the data stored in the properties file in the test script

Read More: Selenium Commands every Developer or Tester must know

1. How to create a Properties file and add data

Please note: Here, it is assumed that the reader has basic knowledge on how to setup java project structure in Eclipse IDE.

QAs first need to set up a basic project structure on eclipse. To create a property file, one needs to follow the steps below:

  • Right-click on Project folder > New > Other> General > FileCreating Object Repository in Selenium | BrowserStack (2)
  • Click Next and then name the file (example: demo_repo.properties) and then click on Finish.

Creating Object Repository in Selenium | BrowserStack (3)
Now, one needs to store the data in this file in key-valuepairs. Just double click on your .properties file and add the key-values.

In this example, we will consider the web elements of the Facebook login page. So we will be adding XPath of username, password, and a login button in our property file.

facebook.login.username.xpath=.//*[@id='email']facebook.login.password.xpath=.//*[@id='pass']facebook.login.Signup.xpath=.//*[@id='loginbutton']

The test case scenario includes three basic steps:

  1. Visiting Facebook login page
  2. Entering username & password
  3. Clicking on login button

2. How to read data from the Properties file

To read data from the Properties file, we need to use the built-in Properties class that is available in Java.util.package.

So one needs to create an object of Properties class:

Properties obj = new Properties();

Now we also need to create an object of FileInputStream class with its path pointing to the .properties file:

FileInputStream objfile = new FileInputStream(System.getProperty("user.dir")+"\\<name of the properties file>");

Now to integrate the properties file in our test script that includes locators, we need to load the file. To do this, we will use the load method of the property class. Refer to the code snippet below.

Properties obj = new Properties();FileInputStream objfile = new FileInputStream(System.getProperty("user.dir")+"\\<name of the properties file>");obj.load(objfile);String username = obj.getProperty("facebook.login.username.xpath");

The string username will contain the XPath to find the username field on the login page.

Try Running Selenium Tests on Cloud for Free

3. Using the Properties file in Selenium Script

Now to use the locator information available in the properties file, we will pass the available data as parameters using the findElement method.

driver.findElement(By.xpath(obj.getProperty("facebook.login.password.xpath"))).sendKeys("adsadasdas");

Refer to the complete code below for getting a sense of the program flow:

import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;import java.util.Properties;import org.openqa.selenium.By;import org.openqa.selenium.WebDriver;import org.openqa.selenium.firefox.FirefoxDriver;import org.testng.annotations.Test;public class TestFacebook {@Testpublic void TestOR() throws IOException{// Specify the file location I used . operation here because//we have object repository inside project directory onlyFile src=new File(".Object_Repo.properties");// Create FileInputStream objectFileInputStream objfile=new FileInputStream(src);// Create Properties class object to read properties fileProperties obj=new Properties();// Load file so we can use into our scriptobj.load(objfile);System.out.println("Property class loaded");// Open FirefoxBrowserWebDriver driver=new FirefoxDriver();// Maximize windowdriver.manage().window().maximize();// Pass applicationdriver.get("http://www.facebook.com");// getProperty Fetches the value of target key from the properties file // In this case entering usernamedriver.findElement(By.xpath(obj.getProperty("facebook.login.username.xpath"))).sendKeys("SeleniumWebDriver@gmail.com");// getProperty Fetches the value of target key from the properties file // In this case entering passworddriver.findElement(By.xpath(obj.getProperty("facebook.login.password.xpath"))).sendKeys("adsadasdas");// getProperty Fetches the value of target key from the properties file // In this case clicking on login buttondriver.findElement(By.xpath(pro.getProperty("facebook.login.Signup.xpath"))).click();}}

Using object repository in Selenium helps QAs manage their test suite more effectively. The centralized approach of maintaining the web element’s locator information makes it easy for testing teams to make changes quickly, with less effort. Thus, an understanding of Object Repository serves to make QA’s lives easier in a significant way.

Follow-up Read: How to Launch Browser in Selenium

Creating Object Repository in Selenium | BrowserStack (2024)
Top Articles
The Big Three and the Tehran Conference
What is Google App Engine? | Definition from TechTarget
Frederick County Craigslist
Directions To Franklin Mills Mall
Lamb Funeral Home Obituaries Columbus Ga
Maria Dolores Franziska Kolowrat Krakowská
Wisconsin Women's Volleyball Team Leaked Pictures
Amtrust Bank Cd Rates
Rainbird Wiring Diagram
Dr Lisa Jones Dvm Married
What Is Njvpdi
Beau John Maloney Houston Tx
The Cure Average Setlist
Ally Joann
91 East Freeway Accident Today 2022
Hobby Stores Near Me Now
Promiseb Discontinued
Raz-Plus Literacy Essentials for PreK-6
At&T Outage Today 2022 Map
Play It Again Sports Norman Photos
kvoa.com | News 4 Tucson
Random Bibleizer
Www.1Tamilmv.con
Does Royal Honey Work For Erectile Dysfunction - SCOBES-AR
Nurofen 400mg Tabletten (24 stuks) | De Online Drogist
Guide to Cost-Benefit Analysis of Investment Projects Economic appraisal tool for Cohesion Policy 2014-2020
Gridwords Factoring 1 Answers Pdf
Armor Crushing Weapon Crossword Clue
What does wym mean?
Royal Caribbean Luggage Tags Pending
Gideon Nicole Riddley Read Online Free
Weekly Math Review Q4 3
CARLY Thank You Notes
New York Rangers Hfboards
Pensacola Cars Craigslist
Pay Entergy Bill
Craigslist Mexicali Cars And Trucks - By Owner
Mugshots Journal Star
Nid Lcms
Birmingham City Schools Clever Login
Anderson Tribute Center Hood River
Unveiling Gali_gool Leaks: Discoveries And Insights
Brake Pads - The Best Front and Rear Brake Pads for Cars, Trucks & SUVs | AutoZone
4k Movie, Streaming, Blu-Ray Disc, and Home Theater Product Reviews & News
Shannon Sharpe Pointing Gif
Ewwwww Gif
The top 10 takeaways from the Harris-Trump presidential debate
Concentrix + Webhelp devient Concentrix
Diamond Desires Nyc
View From My Seat Madison Square Garden
King Fields Mortuary
Laurel Hubbard’s Olympic dream dies under the world’s gaze
Latest Posts
Article information

Author: Terrell Hackett

Last Updated:

Views: 6564

Rating: 4.1 / 5 (52 voted)

Reviews: 91% of readers found this page helpful

Author information

Name: Terrell Hackett

Birthday: 1992-03-17

Address: Suite 453 459 Gibson Squares, East Adriane, AK 71925-5692

Phone: +21811810803470

Job: Chief Representative

Hobby: Board games, Rock climbing, Ghost hunting, Origami, Kabaddi, Mushroom hunting, Gaming

Introduction: My name is Terrell Hackett, I am a gleaming, brainy, courageous, helpful, healthy, cooperative, graceful person who loves writing and wants to share my knowledge and understanding with you.