liveBook · Manning

Locator strategy

As explained in BDD in Action, Second Edition

A locator strategy is an approach for identifying target elements in a web document so automated tests can interact with them.

A locator strategy identifies target elements in a web document so automated tests can interact with them. Centralizing these strategies in a Page Object keeps location logic separate from test intent and gives teams one place to update when the interface changes. section 11.3.1 section 11.2

Choose and organize locator strategies around these practices:

  • Use basic Selenium strategies such as By.id, By.css, or By.xpath when they identify the required element clearly. section 15.2.6
  • Store locators as readable constants or class members instead of embedding selector details throughout test methods. section 11.2
  • Parameterize repeated structures when several elements share the same HTML pattern, such as menu buttons distinguished by their labels. section 11.3.1
  • Compose queries for complex widgets by locating a collection, filtering it by a nested label and matching text, then selecting the first result. section 15.2.6

A login Page Object

Define EMAIL_FIELD, PASSWORD_FIELD, and LOGIN_BUTTON once with By.id, then let signinWithCredentials() use those names. The test supplies credentials and calls the page method without repeating how the elements are located.

Finding a labeled form field

For an Angular Material form without test-friendly identifiers, locate mat-form-field elements, filter for a present mat-label whose text equals the requested name, and use the matching field for an interaction.

How BDD in Action, Second Edition covers it

How deep it goes
Explains basic selectors, centralized constants, parameterized XPath, and composed queries for complex widgets.
Examples
Uses login fields, a parameterized menu bar, and an Angular Material registration field selected by its visible label.
What it assumes
Assumes web-element automation and HTML structure, while demonstrating basic selectors before composing queries for sophisticated interfaces.
What you can do after
Apply it via Page Objects, Selenium WebDriver, or Serenity/JS to centralize selectors and automate interactions with named elements.
How it connects
Connects Page Objects, Selenium WebDriver, XPath, CSS selectors, and Page Element Query Language.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of BDD in Action, Second Edition. The section links point to the parts of the book it draws on.