An Object Repository is a centralized location where all the web elements (objects) and their locators used in test scripts are stored and managed.
Why it matters:
- ✓All locators are in one place — easy to find and update
- ✓Changes to the UI require updating only the repository, not every test
- ✓Promotes reuse — multiple tests can reference the same element
In Selenium, Object Repositories are implemented using:
1. Page Object Model (POM) — each page class IS the object repository for that page:
Java
public class LoginPage {
private By usernameField = By.id("username"); // stored here
private By passwordField = By.id("password");
private By loginButton = By.cssSelector(".btn-login");
}
2. Page Factory with @FindBy — annotations serve as the repository:
Java
@FindBy(id = "username")
WebElement usernameField;
3. Properties file / Excel — store locators externally:
PROPERTIES
# locators.properties
username.locator=id=username
password.locator=id=password
Why not hardcode locators in test methods:
Java
// BAD — locator repeated in every test method
driver.findElement(By.id("username")).sendKeys("admin"); // test 1
driver.findElement(By.id("username")).sendKeys("user2"); // test 2
// GOOD — locator defined once in page class
loginPage.enterUsername("admin");
loginPage.enterUsername("user2");
