Unlike QTP/UFT, Selenium does not offer the default implementation for  object repository. In QTP things are really straightforward, just object spy the controls and add to object repository, and further with the blessings of intellisense feature in its IDE, utilize them easily in writing scripts.

But how we can achieve the same in selenium??..
It can be done by using Properties file feature of Java. Lets begin with some basic explanation.

What is Object Repository?

Object Repository is a centralized location where we can store objects information, it acts as interface between Test script and application in order to identify the objects during the execution.

We always recommend using external file for object repository rather than hard coding the objects and its properties directly into our code. Why this??? As it reduces the maintenance  effort and provides positive ROI, for example say any of the object properties change within our application under test, we can easily change it in external object repository file, rather than searching and doing updates  for that object individually in the code.

A basic object repository can be implemented as a collection of key-value pairs, with the key being a logical name identifying the object and the value containing unique objects properties used to identify the object on a screen. For this, we will use a .properties file in Java which is a basic collection of key-value pairs.

Creating Properties File in Eclipse:-

Right click on the Package in the solution Explorer of Eclipse-> New ->Other

Creating Properties file in Eclipse
Adding New File in Eclipse

Right click on the Package in the solution Explorer of Eclipse-> New ->File (In this case Step-2 below is not required)

Create new file in eclipse
New ->File in Eclipse

General ->File ->Click Next

New file wizard in Eclipse
New Wizard in Eclipse

Give name to the file with .properties extension (Say OR_Gmail_Login.properties) ->Click Finish

Properties file in Java Eclipse
Naming Properties File in Eclipse

We generally create each properties file for every single page and capture all the UI elements present on the page and use it as per the needs.

Adding Key/value Pairs in Properties File:-

Before adding our objects into object repository, let’s outline a simple scenario that we will be automating in Selenium:-

1. Launch Gmail login page.
2. Fill the Username & Password fields and click on Submit button

After creating our empty properties file, now we need to add our elements, in the form of key and Value pairs.
For Example:-

Gmail.LoginPage.txtPassword -> Key

Note: – In Value property we have taken the Locator and its value, which will be used to identify our control. As password field, is getting uniquely identified by ID field so we have taken that value.

Properties file with objects in Selenium
Properties File in Java


package OR;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class PropertiesFile {
public static void main(String[] args) {
//Creating the File Object
File file = new File("D:\Automation\Selenium\MyCode\FirstSeleniumCode\src\OR\OR_Gmail_Login.properties");
//Creating properties object
Properties prop = new Properties();
//Creating InputStream object to read data
FileInputStream objInput = null;
try {
objInput = new FileInputStream(file);
//Reading properties key/values in file
//Closing the InputStream
} catch (FileNotFoundException e) {

} catch (IOException e) {
//Creating the driver instance
WebDriver driver = new FirefoxDriver();
//Adding wait
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
//Launching the GMAIL page
//Entering the UserName
WebElement element = driver.findElement(By.id(prop.getProperty("Gmail.LoginPage.txtUserName")));
//Entering the Password
element = driver.findElement(By.id(prop.getProperty("Gmail.LoginPage.txtPassword")));
//Clicking the SignIn button
element = driver.findElement(By.id(prop.getProperty("Gmail.LoginPage.btnSignIn")));
System.out.println("Test Scenario Completed!!");
//Destroying the driver object


4 Replies to “How to create Object Repository in Selenium”

  1. I'm getting below error;Can any one help me?

    org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"id","selector":".//*[@id='username']"}
    Command duration or timeout: 131.32 seconds
    For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
    Build info: version: '2.53.1', revision: 'a36b8b1cd5757287168e54b817830adce9b0158d', time: '2016-06-30 19:26:09'
    System info: host: 'Dilusha-PC', ip: '', os.name: 'Windows 8.1', os.arch: 'amd64', os.version: '6.3', java.version: '1.8.0_51'
    Driver info: org.openqa.selenium.firefox.FirefoxDriver
    Capabilities [{applicationCacheEnabled=true, rotatable=false, handlesAlerts=true, databaseEnabled=true, version=38.0.5, platform=WINDOWS, nativeEvents=false, acceptSslCerts=true, webStorageEnabled=true, locationContextEnabled=true, browserName=firefox, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true}]
    Session ID: e6ed0239-44c2-4073-8ff0-53631490447f
    *** Element info: {Using=id, value=.//*[@id='username']}

    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
    at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:206)
    at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:158)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:678)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:363)
    at org.openqa.selenium.remote.RemoteWebDriver.findElementById(RemoteWebDriver.java:413)
    at org.openqa.selenium.By$ById.findElement(By.java:218)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:355)
    at CEPKG.BaseClass.Login(BaseClass.java:54)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:86)
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:643)
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:820)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1128)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:129)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:112)
    at org.testng.TestRunner.privateRun(TestRunner.java:782)
    at org.testng.TestRunner.run(TestRunner.java:632)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:366)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:361)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:319)
    at org.testng.SuiteRunner.run(SuiteRunner.java:268)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1244)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1169)
    at org.testng.TestNG.run(TestNG.java:1064)
    at org.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:74)
    at org.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:124)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)

Leave a Reply

Your email address will not be published. Required fields are marked *