17. Resilient User-First Locators & Filtering

Recommended Locator Priority

1. page.getByRole(role, { name }): Accessible buttons, links, headings. 2. page.getByLabel("Email"): Inputs associated with <label>. 3. page.getByPlaceholder("Search"): Inputs without visible labels. 4. page.getByText("Order Placed"): Static text. 5. page.getByTestId("checkout-btn"): Dedicated QA test id.
// Locator Chaining & Filtering:
const targetCard = page.locator(".product-card")
  .filter({ hasText: "MacBook Pro" });
await targetCard.getByRole("button", { name: "Add to Cart" }).click();

Task

  • Implement buildTableRowSelector:
  • 1. Accept orderId string.
  • 2. Return `tr:has-text("${orderId}") >> button:has-text("Delete")`.
πŸ’‘Common doubts
Did you like the problem?
Lesson 17 of 35
JavaScript
Loading code editor…
Next