Skip to main content

Overview

The TextFieldElement class is a Playwright wrapper for the <vaadin-text-field> component. It provides methods to interact with text fields, including setting values, validating input, and accessing field properties.

Component Tag

<vaadin-text-field>

Extends

  • VaadinElement

Implements

  • HasValidationPropertiesElement - Validation state and error messages
  • HasInputFieldElement - Value, label, helper text, and styling
  • HasPrefixElement - Prefix slot content
  • HasSuffixElement - Suffix slot content
  • HasClearButtonElement - Clear button functionality
  • HasPlaceholderElement - Placeholder text
  • HasAllowedCharPatternElement - Character input restrictions
  • HasThemeElement - Theme variants
  • FocusableElement - Focus and blur operations
  • HasAriaLabelElement - ARIA label support
  • HasEnabledElement - Enabled/disabled state
  • HasTooltipElement - Tooltip text

Constructor

locator
Locator
required
The Playwright locator for the <vaadin-text-field> element
public TextFieldElement(Locator locator)

Factory Methods

getByLabel (Page)

Get a text field by its accessible label. Uses ARIA role textbox.
page
Page
required
The Playwright page
label
String
required
The accessible label of the text field
public static TextFieldElement getByLabel(Page page, String label)

Example

TextFieldElement textField = TextFieldElement.getByLabel(page, "Username");
textField.setValue("john.doe");
textField.assertValue("john.doe");

getByLabel (Locator)

Get a text field by its accessible label within a scope.
locator
Locator
required
The locator to search within
label
String
required
The accessible label of the text field
public static TextFieldElement getByLabel(Locator locator, String label)

Methods

getMinLength

Get the current minimum length constraint.
public Integer getMinLength()
Returns: The minimum length or null if not set

setMinLength

Set the minimum length constraint.
min
int
required
The minimum length
public void setMinLength(int min)

assertMinLength

Assert the minimum length matches the expected value.
min
Integer
The expected minimum length, or null to assert no minimum is set
public void assertMinLength(Integer min)

Example

TextFieldElement textField = TextFieldElement.getByLabel(page, "Validated Textfield");
textField.assertMinLength(6);
assertEquals(6, textField.getMinLength());

getMaxLength

Get the current maximum length constraint.
public Integer getMaxLength()
Returns: The maximum length or null if not set

setMaxLength

Set the maximum length constraint.
max
int
required
The maximum length
public void setMaxLength(int max)

assertMaxLength

Assert the maximum length matches the expected value.
max
Integer
The expected maximum length, or null to assert no maximum is set
public void assertMaxLength(Integer max)

Example

TextFieldElement textField = TextFieldElement.getByLabel(page, "Validated Textfield");
textField.assertMaxLength(7);
assertEquals(7, textField.getMaxLength());

getPattern

Get the current validation pattern.
public String getPattern()
Returns: The pattern or null if not set

setPattern

Set the validation pattern.
pattern
String
required
The regex pattern
public void setPattern(String pattern)

assertPattern

Assert the pattern matches the expected value.
pattern
String
The expected pattern, or null to assert no pattern is set
public void assertPattern(String pattern)

Example

TextFieldElement textField = TextFieldElement.getByLabel(page, "Validated Textfield");
textField.assertPattern("\\d{7}");
assertEquals("\\d{7}", textField.getPattern());

getFocusLocator

Get the locator for focus operations. Returns the input locator.
public Locator getFocusLocator()

getAriaLabelLocator

Get the locator for ARIA label operations. Returns the input locator.
public Locator getAriaLabelLocator()

getEnabledLocator

Get the locator for enabled/disabled state. Returns the input locator.
public Locator getEnabledLocator()

Inherited Methods

From HasValueElement:
  • getValue() - Get current value
  • setValue(String value) - Set field value
  • clear() - Clear the value
  • assertValue(String value) - Assert value matches
  • getInputLocator() - Get input element locator
From HasValidationPropertiesElement:
  • assertValid() - Assert field is valid
  • assertInvalid() - Assert field is invalid
  • assertErrorMessage(String message) - Assert error message text
  • getErrorMessageLocator() - Get error message locator
From HasClearButtonElement:
  • clickClearButton() - Click the clear button
  • isClearButtonVisible() - Check if clear button is visible
  • assertClearButtonVisible() - Assert clear button is visible
  • assertClearButtonNotVisible() - Assert clear button is not visible
From HasPlaceholderElement:
  • getPlaceholder() - Get placeholder text
  • setPlaceholder(String placeholder) - Set placeholder text
  • assertPlaceholder(String placeholder) - Assert placeholder matches
From FocusableElement:
  • focus() - Focus the field
  • blur() - Blur the field
  • getTabIndex() - Get tab index
  • assertIsFocused() - Assert field has focus
  • assertIsNotFocused() - Assert field does not have focus
From HasLabelElement:
  • getLabel() - Get label text
  • assertLabel(String label) - Assert label text
  • getLabelLocator() - Get label locator
From HasHelperElement:
  • getHelperText() - Get helper text
  • assertHelperHasText(String text) - Assert helper text
  • getHelperLocator() - Get helper locator

Complete Example

@Test
public void testTextFieldValidation() {
    // Find text field by label
    TextFieldElement textField = TextFieldElement.getByLabel(page, "Validated Textfield");
    textField.assertVisible();
    
    // Check validation constraints
    textField.assertMinLength(6);
    textField.assertMaxLength(7);
    textField.assertPattern("\\d{7}");
    textField.assertAllowedCharPattern("[0-8]");
    
    // Test invalid value
    textField.setValue("1");
    textField.assertInvalid();
    textField.assertErrorMessage("Minimum length is 6 characters");
    
    // Test valid value
    textField.setValue("1238456");
    textField.assertValid();
    textField.assertValue("1238456");
}

@Test
public void testTextFieldInteractions() {
    TextFieldElement textField = TextFieldElement.getByLabel(page, "TextField with placeholder and clear button");
    
    // Check placeholder
    textField.assertPlaceholder("Enter text here");
    
    // Test clear button
    textField.assertClearButtonNotVisible();
    textField.setValue("some value");
    textField.assertClearButtonVisible();
    textField.clickClearButton();
    textField.assertValue("");
    
    // Test prefix and suffix
    textField.assertPrefixHasText("Prefix");
    textField.assertSuffixHasText("Suffix");
}

Build docs developers (and LLMs) love