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.
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”
QAs can create two types of object repositories in Selenium WebDriver:
- Using a properties file in Selenium
- Using an XML file
Further, let us understand three things:
- How to create and store data in the Properties file
- How to read data from the Properties file
- Using the data stored in the properties file in the test script
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 > File
- Click Next and then name the file (example: demo_repo.properties) and then click on Finish.
Now, one needs to store the data in this file in key-value pairs. 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:
- Visiting Facebook login page
- Entering username & password
- 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 { @Test public void TestOR() throws IOException{ // Specify the file location I used . operation here because //we have object repository inside project directory only File src=new File(".Object_Repo.properties"); // Create FileInputStream object FileInputStream objfile=new FileInputStream(src); // Create Properties class object to read properties file Properties obj=new Properties(); // Load file so we can use into our script obj.load(objfile); System.out.println("Property class loaded"); // Open FirefoxBrowser WebDriver driver=new FirefoxDriver(); // Maximize window driver.manage().window().maximize(); // Pass application driver.get("http://www.facebook.com"); // getProperty Fetches the value of target key from the properties file // In this case entering username driver.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 password driver.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 button driver.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