8. User-First Locators with Java AriaRole

Strongly Typed AriaRole Locators

import com.microsoft.playwright.options.AriaRole;

// Target accessible button:
Locator submitBtn = page.getByRole(
AriaRole.BUTTON,
new Page.GetByRoleOptions().setName("Submit")
);

// Form inputs:
Locator emailInput = page.getByLabel("Email Address");
Locator searchInput = page.getByPlaceholder("Search catalog");

// Chaining:
Locator activeRow = page.locator("tr")
.filter(new Locator.FilterOptions().setHasText("Order #401"))
.getByRole(AriaRole.BUTTON, new Locator.GetByRoleOptions().setName("View"));

Task

  • Implement formatRoleQuery:
  • 1. Accept String role and String name.
  • 2. Return role + "[" + name + "]".
πŸ’‘Common doubts
Did you like the problem?
Lesson 8 of 18
Java
Loading code editor…
Next