17. User-Facing Locators & Resilient Query Strategies

Best Practice Locator Hierarchy

1. page.getByRole(AriaRole.BUTTON, new Page.GetByRoleOptions().setName("Submit")) 2. page.getByLabel("Email Address") 3. page.getByPlaceholder("Enter your password") 4. page.getByText("Welcome back") 5. page.getByTestId("checkout-btn")
import com.microsoft.playwright.*;
import com.microsoft.playwright.options.AriaRole;

Locator submitBtn = page.getByRole(AriaRole.BUTTON,
new Page.GetByRoleOptions().setName("Confirm Payment"));

Task

  • Write a method getSubmitButton:
  • 1. Accept Page page and String buttonName.
  • 2. Return page.getByRole(AriaRole.BUTTON, new Page.GetByRoleOptions().setName(buttonName)).
πŸ’‘Common doubts
Did you like the problem?
Lesson 17 of 38
Java
Loading code editor…
Next