Skip to main content

Overview

FocusableElement provides methods for managing keyboard focus on Vaadin components. It allows you to programmatically focus and blur elements, retrieve tab index values, and assert focus state in tests. Interface Location: org.vaadin.addons.dramafinder.element.shared.FocusableElement

Methods

getFocusLocator()

Returns the locator to focus or blur. By default, returns the component root, but implementations may override this to target a specific internal element (e.g., an input field within a larger component). Returns: Locator - The locator to receive focus operations

focus()

Programmatically sets keyboard focus to the component.

blur()

Programmatically removes keyboard focus from the component.

getTabIndex()

Retrieves the current tab index as a string from the tabIndex attribute. Returns: String - The tab index value, or null if not set

assertIsFocused()

Asserts that the component currently has keyboard focus. Throws: AssertionError if the component is not focused

assertIsNotFocused()

Asserts that the component does not have keyboard focus. Throws: AssertionError if the component is focused

Implementing Classes

The following element classes implement FocusableElement:
  • ButtonElement
  • CheckboxElement
  • RadioButtonElement
  • TextFieldElement (and all subclasses)
  • ComboBoxElement
  • MultiSelectComboBoxElement
  • SelectElement
  • MessageInputElement
  • GridElement
  • VirtualListElement
  • AvatarElement

Usage Example

Implementation Details

Locator Delegation Pattern

Many components override getFocusLocator() to delegate focus operations to an internal element. For example, text fields delegate to their input element:
This ensures focus is applied to the correct interactive element within the component’s shadow DOM structure.

Tab Index Management

The getTabIndex() method retrieves the tabIndex attribute, which controls the tab order of focusable elements:
  • 0 - Natural tab order (default for interactive elements)
  • Positive number - Custom tab order (lower numbers are focused first)
  • -1 - Programmatically focusable but not in tab order

Testing Patterns

Form Navigation Testing

Validation on Blur

Focus After Dialog Open

Testing Autofocus

Best Practices

Use blur() to Trigger Validation

Many Vaadin components perform validation when focus is lost. Always call blur() after setting a value if you want to test validation:

Check Focus State Before Interaction

Before performing keyboard input, ensure the element has focus:

Test Tab Order

Verify that users can navigate through your form using the Tab key in the expected order: