Object Repository in Selenium Java
1. What is an Object Repository in Selenium?
An Object Repository (OR) in Selenium Java is a centralized location where we store web application element locators such as:
- ID
- Name
- Class Name
- XPath
- CSS Selector
- Link Text
- Partial Link Text
- Tag Name
Instead of hardcoding locators directly inside Selenium test scripts, we maintain them separately in an Object Repository.
This approach makes automation frameworks easier to maintain, reusable, scalable, and manageable.
Example:
Without Object Repository:
driver.findElement(By.id("username")).sendKeys("admin");
driver.findElement(By.id("password")).sendKeys("admin123");
driver.findElement(By.id("login")).click();
With Object Repository:
driver.findElement(By.id(OR.getProperty("username")))
.sendKeys("admin");
driver.findElement(By.id(OR.getProperty("password")))
.sendKeys("admin123");
driver.findElement(By.id(OR.getProperty("login")))
.click();
Here, locators are maintained separately in a properties file.
2. Why do we need an Object Repository?
The main advantages of using an Object Repository are:
- Maintainability: If a locator changes, we only need to update the properties file.
- Reusability: The same locator can be reused across multiple test cases.
- Readability: Test scripts become cleaner and easier to understand.
- Centralization: All application locators are maintained in one place.
- Scalability: It becomes easier to manage large automation frameworks.
- Reduced duplication: We avoid writing the same locator multiple times.
3. Types of Object Repository in Selenium
There are different ways to implement an Object Repository in Selenium Java.
| Type | Description |
|---|---|
| Properties File | Stores locators as key-value pairs. |
| XML File | Stores locators in XML format. |
| Excel File | Stores locators in Excel sheets. |
| JSON File | Stores locators in JSON format. |
| Java Class | Stores locators using static variables or methods. |
| Page Object Model (POM) | Maintains locators and page actions in separate page classes. |
Let's understand these approaches with examples.
4. Object Repository Using Properties File
A properties file is one of the most commonly used approaches for maintaining locators in Selenium Java.
Step 1: Create a Properties File
Create a file named ObjectRepository.properties.
Project structure:
SeleniumFramework
│
├── src/test/java
│ └── tests
│ └── LoginTest.java
│
├── src/main/java
│ └── utilities
│ └── ConfigReader.java
│
└── src/test/resources
└── ObjectRepository.properties
Step 2: Define Locators in Properties File
ObjectRepository.properties
# Login Page Locators
username=id:username
password=id:password
loginButton=xpath://button[@type='submit']
# Home Page Locators
homePageTitle=css:h1.page-title
logoutButton=id:logout
In this file:
usernameis the locator key.id:usernamerepresents the locator strategy and value.passwordis another locator key.id:passwordrepresents its locator value.
Step 3: Create ConfigReader Class
Create a Java class to read the properties file.
ConfigReader.java
package utilities;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
public class ConfigReader {
private static Properties prop = new Properties();
static {
try {
FileInputStream file = new FileInputStream(
"src/test/resources/ObjectRepository.properties"
);
prop.load(file);
file.close();
} catch (IOException e) {
throw new RuntimeException(
"Unable to load Object Repository", e
);
}
}
public static String getProperty(String key) {
return prop.getProperty(key);
}
}
Explanation:
Properties: Java class used to manage key-value pairs.FileInputStream: Reads the properties file.load(): Loads the properties into memory.getProperty(): Retrieves a locator using its key.static: Loads the properties file once when the class is initialized.
Step 4: Create a Login Test Class
LoginTest.java
package tests;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import utilities.ConfigReader;
public class LoginTest {
public static void main(String[] args) {
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://example.com/login");
driver.findElement(
By.id(ConfigReader.getProperty("username"))
).sendKeys("admin");
driver.findElement(
By.id(ConfigReader.getProperty("password"))
).sendKeys("admin123");
driver.findElement(
By.xpath(ConfigReader.getProperty("loginButton"))
).click();
driver.quit();
}
}
Note: The above example stores the locator strategy and value together. In this particular implementation, use the following updated properties file format to match the
Bymethods:
username=username
password=password
loginButton=//button[@type='submit']
5. Object Repository Using XML File
Another approach is to maintain locators in an XML file.
Step 1: Create XML File
Create ObjectRepository.xml.
<?xml version="1.0" encoding="UTF-8"?>
<locators>
<element name="username">
<type>id</type>
<value>username</value>
</element>
<element name="password">
<type>id</type>
<value>password</value>
</element>
<element name="loginButton">
<type>xpath</type>
<value>//button[@type='submit']</value>
</element>
</locators>
Step 2: Read XML File Using Java
import org.w3c.dom.*;
import javax.xml.parsers.*;
import java.io.File;
public class XMLReader {
public static String getLocator(String name, String tag) {
try {
File file = new File(
"src/test/resources/ObjectRepository.xml"
);
DocumentBuilderFactory factory =
DocumentBuilderFactory.newInstance();
DocumentBuilder builder =
factory.newDocumentBuilder();
Document document = builder.parse(file);
NodeList nodes =
document.getElementsByTagName("element");
for (int i = 0; i < nodes.getLength(); i++) {
Element element = (Element) nodes.item(i);
if (element.getAttribute("name").equals(name)) {
return element
.getElementsByTagName(tag)
.item(0)
.getTextContent();
}
}
} catch (Exception e) {
throw new RuntimeException(e);
}
return null;
}
}
Step 3: Use XML Repository in Selenium
WebDriver driver = new ChromeDriver();
driver.get("https://example.com/login");
String username = XMLReader.getLocator(
"username", "value"
);
driver.findElement(By.id(username))
.sendKeys("admin");
driver.quit();
6. Object Repository Using Java Class
We can also maintain locators directly inside Java classes using static variables.
LoginPage.java
package pages;
import org.openqa.selenium.By;
public class LoginPage {
public static By username =
By.id("username");
public static By password =
By.id("password");
public static By loginButton =
By.xpath("//button[@type='submit']");
}
LoginTest.java
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import pages.LoginPage;
public class LoginTest {
public static void main(String[] args) {
WebDriver driver = new ChromeDriver();
driver.get("https://example.com/login");
driver.findElement(LoginPage.username)
.sendKeys("admin");
driver.findElement(LoginPage.password)
.sendKeys("admin123");
driver.findElement(LoginPage.loginButton)
.click();
driver.quit();
}
}
Advantages:
- Easy to implement.
- No external file required.
- Compile-time support.
- Easy integration with Page Object Model.
7. Object Repository Using Page Object Model (POM)
Page Object Model is a design pattern in Selenium where each application page is represented by a separate Java class.
Each page class contains:
- Web element locators.
- Page-specific methods.
- Reusable actions.
Project structure:
SeleniumFramework
│
├── src/test/java
│ ├── pages
│ │ └── LoginPage.java
│ │
│ └── tests
│ └── LoginTest.java
LoginPage.java
package pages;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
public class LoginPage {
WebDriver driver;
By username = By.id("username");
By password = By.id("password");
By loginButton = By.id("login");
public LoginPage(WebDriver driver) {
this.driver = driver;
}
public void enterUsername(String user) {
driver.findElement(username).sendKeys(user);
}
public void enterPassword(String pass) {
driver.findElement(password).sendKeys(pass);
}
public void clickLogin() {
driver.findElement(loginButton).click();
}
public void login(String user, String pass) {
enterUsername(user);
enterPassword(pass);
clickLogin();
}
}
LoginTest.java
package tests;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import pages.LoginPage;
public class LoginTest {
public static void main(String[] args) {
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://example.com/login");
LoginPage loginPage = new LoginPage(driver);
loginPage.login("admin", "admin123");
driver.quit();
}
}
Advantages of POM:
- Separates test logic from page logic.
- Improves code reusability.
- Makes maintenance easier.
- Supports large automation frameworks.
- Reduces duplicate code.
8. Difference Between Object Repository and Page Object Model
| Object Repository | Page Object Model |
|---|---|
| Stores application locators. | Represents application pages as classes. |
| Can use properties, XML, JSON, or Java files. | Uses Java classes. |
| Mainly manages locators. | Manages locators and page actions. |
| Does not necessarily contain business methods. | Contains reusable page methods. |
| Can be used with POM. | Can use an Object Repository internally. |
9. Best Practices for Object Repository
- Maintain meaningful locator names.
- Prefer unique IDs and stable CSS selectors.
- Avoid absolute XPath wherever possible.
- Keep locators centralized.
- Avoid duplicate locator definitions.
- Separate application configuration from element locators.
- Use Page Object Model for large frameworks.
- Avoid hardcoding test data inside locator files.
- Use reusable utility methods.
- Keep sensitive credentials outside source-controlled files.
10. Interview Questions on Object Repository
Q1. What is an Object Repository in Selenium?
An Object Repository is a centralized location used to maintain web element locators separately from automation scripts.
Q2. What are the different types of Object Repository?
Common approaches include Properties files, XML files, Excel files, JSON files, and Java classes.
Q3. Which Object Repository approach is commonly used in Selenium Java?
Properties files and Java-based Page Object Model implementations are commonly used.
Q4. What is the difference between Properties file and XML file?
A Properties file stores simple key-value pairs, whereas XML stores structured hierarchical data.
Q5. Can we use Object Repository with Page Object Model?
Yes. We can maintain locators in an external Object Repository and use them inside Page Object Model classes.
Q6. What is the advantage of using an Object Repository?
It improves maintainability, readability, reusability, and scalability.
Q7. Is Object Repository mandatory in Selenium?
No. Selenium does not mandate an Object Repository. It is an automation framework design approach.
Q8. What happens if a locator changes in an Object Repository?
We update the locator in its centralized location instead of modifying every test script that uses it.
11. Recommended Approach for Real-Time Automation Frameworks
For a professional Selenium Java automation framework, I recommend combining:
- Page Object Model (POM)
- Properties file for environment configuration
- Java page classes for locators and actions
- TestNG for test execution
- Maven for dependency management
- Extent Reports for reporting
- Selenium WebDriverWait for synchronization
This combination provides a maintainable and scalable automation framework.

No comments:
Post a Comment