Drama Finder provides static factory methods on element classes to locate Vaadin components using accessibility-first patterns. Instead of writing complex CSS selectors or XPath expressions, you can find elements by their accessible names, labels, and text content.
Most input fields support getByLabel() which finds elements by their accessible label:
// Find a text field by its labelTextFieldElement nameField = TextFieldElement.getByLabel(page, "Full Name");// Find a date picker by its labelDatePickerElement birthDate = DatePickerElement.getByLabel(page, "Birth Date");// Find a checkbox by its labelCheckboxElement termsCheckbox = CheckboxElement.getByLabel(page, "I agree to the terms");
Factory methods use ARIA roles internally to ensure accurate matching. For example, TextFieldElement.getByLabel() searches for elements with the TEXTBOX role.
Buttons and other text-based elements support getByText() to find elements by their visible text:
// Find a button by its textButtonElement saveButton = ButtonElement.getByText(page, "Save");ButtonElement cancelButton = ButtonElement.getByText(page, "Cancel");// getByLabel is an alias for getByText on buttonsButtonElement submitButton = ButtonElement.getByLabel(page, "Submit");
Some elements like Grid provide additional lookup methods:
// Grid lookup by IDGridElement grid = GridElement.getById(page, "user-grid");// First grid on the pageGridElement firstGrid = GridElement.get(page);// Grid within a containerLocator container = page.locator(".data-panel");GridElement scopedGrid = GridElement.get(container);
For elements without dedicated factory methods, construct them directly:
// Direct construction with custom locatorAccordionElement accordion = new AccordionElement(page.locator(".custom-css"));
Factory methods that accept custom role options provide flexibility for complex scenarios:
// Button with exact name matchingButtonElement exactButton = ButtonElement.getByText(page, new Page.GetByRoleOptions() .setName("Save") .setExact(true));// Button that is not disabledButtonElement enabledButton = ButtonElement.getByText(page, new Page.GetByRoleOptions() .setName("Submit") .setDisabled(false));
Always use factory methods that leverage accessible names (labels, text content) rather than CSS selectors or XPath. This makes tests more maintainable and accessible.
// Good: Accessible and maintainableTextFieldElement nameField = TextFieldElement.getByLabel(page, "Full Name");// Bad: Brittle and not accessibility-focusedTextFieldElement nameField = new TextFieldElement(page.locator("#name-field"));
Use .first() for non-unique matches
When your locator might match multiple elements, always use .first() or scope your query:
// Locator automatically uses .first() in factory methodsButtonElement button = ButtonElement.getByText(page, "Submit");// When constructing directly, add .first()ButtonElement firstButton = new ButtonElement( page.locator("vaadin-button").first());
Scope lookups to reduce ambiguity
When multiple elements share the same label or text, use scoped lookups:
// Scope to a specific dialog or containerLocator dialogLocator = page.locator("vaadin-dialog[opened]");ButtonElement dialogButton = ButtonElement.getByText(dialogLocator, "OK");