Implement Object Repository In Selenium WebDriver

Selenium Webdriver is the most used software automation testing tool. It provides APIs that operates on the web objects using their element locators like ID, CSS, XPath, etc. It doesn’t have any built-in feature to implement object repository.

Lacking this facility increases the efforts of maintaining the objects locators. If you hard code these elements locators in the source code that make it more unmanageable. Also, this approach leads to frequent code changes whenever the locator gets changed. Hence, test automation doesn’t get fixed until the UI gets finalized.

If you are using an Id (which is one of the types of element locators) in multiple test cases and it gets modified. Then, you would need to update its value in all test cases. Unfortunately, if you miss updating any of them, then the test case eventually will fail.

Using Properties File in Selenium

Benefits of Using Properties File

You can overcome all of such issues with the help of an object repository. You can implement the object repository using Java’s Properties class which is a part of the Java Util package. You can store the element locators as the key-value pair in a property file. You can even use it to hold project configuration properties like username/passwords etc.

Username_field =name:username
Password_field =name:password
Login_button =classname:loginbtn

Let’s now check out how can we implement object repository using the properties file in Selenium WebDriver.

Firstly, we’ll see one of the simplest methods to create a property file. Just make sure you have Eclipse installed on your system. We’ve tested the example code using the Eclipse IDE.

Implement Object Repository from a Properties File

It can be quickly done using the Eclipse IDE. Open the Eclipse and navigate to File Menu >> New >> Click on the File option.

Implement Object Repository

Implement Object Repository – Select a file.

The only thing you need to do is to set the file name and add the extension as <.properties>. (Example:

Implement Object Repository

Implement Object Repository – Specify File Name

Now you can open it in the Eclipse using the Property file editor where you can add/edit any key-value pair. Though, it is a simple text file which you can modify using the basic file editors like the Notepad.

Read a Properties File

In Selenium projects, the main purpose of the “.properties” files is to store the GUI locators/elements, project configuration data, and database configuration, etc.

Each parameter in properties file appears as a pair of strings, in key-value format, where each key is on one line followed by its value separated by some delimiter. You can refer the sample properties file from the previous section.

Below is an example program which demonstrates to read the data from the .properties file using Java.

To execute the sample code in Java Eclipse, create a package named ‘seleniumObjectMap’ and add the class file ‘ReadFileData’ in this package. Also, create a folder named <Resources> and add a file named <> in this folder. Please see the attached snapshot below which provides a glimpse of the folder structure used for the sample project.

Make sure that you add Selenium Java Webdriver jar files as referenced in the project build path. For details on how to create a Selenium test project refer our -> post on this topic.

Implement Object Repository

Implement Object Repository – Reading properties file.



Implement Object Repository – Reading

Sample Source Code

package seleniumObjectMap;

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 ReadFileData {

	public static void main(String[] args) {
		final String propFile = System.getProperty("user.dir")
				+ "\resources\";
		File file = new File(propFile);
		FileInputStream fileInput = null;
		try {
			fileInput = new FileInputStream(file);
		} catch (FileNotFoundException e) {
		Properties prop = new Properties();
		try {
		} catch (IOException e) {

		WebDriver driver = new FirefoxDriver();
		driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);

		System.out.println("URL ::" + prop.getProperty("url"));
		System.out.println("User name::" + prop.getProperty("username"));
		System.out.println("Password::" + prop.getProperty("password"));



We’d passed the properties values to the Webdriver and printed the values at the end. Check the below Output after executing the above program.