Skip to main content

Overview

HasLabelElement provides methods for interacting with visible label elements in Vaadin components. Labels are essential for accessibility and usability, providing clear identification of form fields and other interactive elements. Interface Location: org.vaadin.addons.dramafinder.element.shared.HasLabelElement

Methods

getLabelLocator()

Returns a locator for the visible label element. By default, this targets the first <label> element within the component. Returns: Locator - Locator for the label element

getLabel()

Retrieves the text content of the label. Returns: String - The label text

assertLabel()

Asserts that the label text matches the expected value. When null is provided, asserts that the label is hidden.
String
The expected label text, or null to assert the label is hidden
Throws: AssertionError if the label text does not match or visibility does not match expectation

Implementing Classes

The following element classes implement HasLabelElement:
  • All text input components (via HasInputFieldElement)
  • RadioButtonGroupElement
  • SideNavigationElement
  • SideNavigationItemElement

Usage Example

Implementation Details

Label Locator Strategy

The default implementation targets the first <label> element within the component:
This works for most Vaadin components that render labels in their shadow DOM.

Inner Text Usage

The assertLabel() method uses setUseInnerText(true) to properly handle nested elements within labels:
This ensures that labels with rich content (spans, icons, etc.) are correctly matched.

Null Handling

When assertLabel(null) is called, the method asserts that the label is hidden rather than absent:
This distinguishes between:
  • No label element (would cause an error)
  • Hidden label element (passes the assertion)

Testing Patterns

Test Label Content

Test Required Field Indicators

Test Label Visibility

Test Label Internationalization

Test Dynamic Label Changes

Best Practices

Always Provide Visible Labels

For accessibility, most form fields should have visible labels:

Use Descriptive Labels

Test that labels clearly describe the field purpose:

Test Label-Field Association

Verify labels are properly associated with their fields:

Don’t Confuse Label with Placeholder

Labels and placeholders serve different purposes:

Accessibility Considerations

Label for Screen Readers

Labels are crucial for screen reader accessibility. The <label> element should be properly associated with the input:

Required Field Indication

Test that required fields are clearly marked in labels:

Label vs ARIA Label

Visible labels (HasLabelElement) are different from ARIA labels (HasAriaLabelElement):

Common Use Cases

Form Field Labels

Radio Button Group Labels