Real World Example

Locators File at a Retail Project

Large teams keep all locators of a page in one place so a UI change means editing one line. For the ShopEase search page:

public final class SearchLocators {
    public static final By SEARCH_BOX    = By.id("search");
    public static final By PRODUCT_CARDS = By.cssSelector(".product-card");
    public static final By PRODUCT_NAME  = By.cssSelector(".product-card h3");
    public static final By ADD_TO_CART   = By.id("add-to-cart");
    public static final By CART_COUNT    = By.id("cart-count");
    public static final By NO_RESULTS    = By.xpath("//p[contains(text(),'No products found')]");
    private SearchLocators() {}
}

driver.findElement(SearchLocators.SEARCH_BOX).sendKeys("laptop\n");
System.out.println(driver.findElements(SearchLocators.PRODUCT_CARDS).size() + " laptops");

Example

Output:

Run SearchTest.main()

Example output

18 laptops

Why It Helps

When ShopEase renamed #search to #site-search in a redesign, the team changed one constant and all 40 search tests passed again. In the advance level this idea grows into the Page Object Model.