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()
<label> element within the component.
Returns: Locator - Locator for the label element
getLabel()
String - The label text
assertLabel()
null is provided, asserts that the label is hidden.
String
The expected label text, or
null to assert the label is hiddenAssertionError if the label text does not match or visibility does not match expectation
Implementing Classes
The following element classes implementHasLabelElement:
- All text input components (via
HasInputFieldElement) RadioButtonGroupElementSideNavigationElementSideNavigationItemElement
Usage Example
Implementation Details
Label Locator Strategy
The default implementation targets the first<label> element within the component:
Inner Text Usage
TheassertLabel() method uses setUseInnerText(true) to properly handle nested elements within labels:
Null Handling
WhenassertLabel(null) is called, the method asserts that the label is hidden rather than absent:
- 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
Navigation Item Labels
Related Interfaces
- HasAriaLabelElement - For accessible names (may differ from visible label)
- HasHelperElement - For additional help text below the field
- HasPlaceholderElement - For placeholder text within the field
- HasInputFieldElement - Composite interface that includes label support