Class ComponentTester<T extends Component>

java.lang.Object
com.vaadin.testbench.unit.ComponentTester<T>
Type Parameters:
T - component type
Direct Known Subclasses:
AbstractLoginTester, AccordionTester, ButtonTester, ChartTester, CheckboxGroupTester, CheckboxTester, ComboBoxTester, ConfirmDialogTester, ContextMenuTester, DatePickerTester, DateTimePickerTester, DetailsTester, DialogTester, GridTester, HtmlComponentTester, InputTester, ListBoxTester, MenuBarTester, MessageInputTester, MessageListTester, MultiSelectComboBoxTester, MultiSelectListBoxTester, NotificationTester, NumberFieldTester, RadioButtonGroupTester, RadioButtonTester, RangeInputTester, SelectTester, SideNavTester, TabSheetTester, TabsTester, TextAreaTester, TextFieldTester, TimePickerTester, UploadTester

public class ComponentTester<T extends Component> extends Object
Test wrapper for components with helpful methods for testing a component.

More targeted methods for specific components exist in named component wrappers.

  • Constructor Details

    • ComponentTester

      public ComponentTester(T component)
      Wrap given component for testing.
      Parameters:
      component - target component
  • Method Details

    • getComponent

      public T getComponent()
      Get the wrapped component.
      Returns:
      wrapped component
    • isUsable

      public boolean isUsable()
      Validate that component can be interacted with and should be visible in the UI. Subclasses overriding this method should also override notUsableReasons(Consumer) to provide additional details to the potential exception thrown by ensureComponentIsUsable().
      Returns:
      true if component can be interacted with by the user
      See Also:
    • isUsable

      protected static boolean isUsable(Component component)
      Validate that the given component can be interacted with and should be visible in the UI. Subclasses overriding this method should also override notUsableReasons(Consumer) to provide additional details to the potential exception thrown by ensureComponentIsUsable().
      Returns:
      true if component can be interacted with by the user
      See Also:
    • setModal

      public void setModal(boolean modal)
      Set component modality.

      Automatically generates a client side change to propagate modality.

      Parameters:
      modal - true to make component modal, false to remove modality
    • find

      public <R extends Component> ComponentQuery<R> find(Class<R> componentType)
      Gets a ComponentQuery to search for component of the given type nested inside the wrapped component.
      Type Parameters:
      R - type of the component to search.
      Parameters:
      componentType - type of the component to search.
      Returns:
      a ComponentQuery instance, searching for wrapped component children.
    • ensureComponentIsUsable

      protected final void ensureComponentIsUsable()
      Checks that wrapped component is usable, otherwise throws an IllegalStateException with details on the current state of the component.
    • ensureComponentIsUsable

      protected static void ensureComponentIsUsable(Component component, Predicate<Component> usableTest)
      Throws an IllegalStateException with details on the current state of the component if it is not usable according to the provided test.
      Parameters:
      component - the component to check
      usableTest - function that tests if the component is usable or not.
    • notUsableReasons

      protected void notUsableReasons(Consumer<String> collector)
      Provides messages explaining why the component is actually not usable. Subclasses overriding isUsable() should also override this method to provide additional details to the potential exception throw by ensureComponentIsUsable().
      See Also:
    • notUsableReasons

      protected static void notUsableReasons(Component component, Consumer<String> collector)
      Provides messages explaining why the given component is actually not usable. Subclasses overriding isUsable() should also override this method to provide additional details to the potential exception throw by ensureComponentIsUsable().
      See Also:
    • ensureVisible

      protected void ensureVisible()
      Check that the component is visible for the user. Else throw an IllegalStateException
    • ensureVisible

      protected static void ensureVisible(Component component)
      Check that the given component is visible for the user. Else throw an IllegalStateException
    • roundTrip

      protected void roundTrip()
      Simulates a server round-trip, flushing pending component changes.
    • getField

      protected Field getField(String fieldName)
      Get field with given name in the wrapped component.
      Parameters:
      fieldName - field name
      Returns:
      accessible field
      Throws:
      IllegalArgumentException - if field doesn't exist
    • getField

      protected Field getField(Class target, String fieldName)
      Get field with given name in the given class.
      Parameters:
      target - class to get field from
      fieldName - field name
      Returns:
      accessible field
      Throws:
      IllegalArgumentException - if field doesn't exist
    • getMethod

      protected Method getMethod(String methodName, Class<?>... parameterTypes)
      Get method with given name and parameters in the wrapped component.
      Parameters:
      methodName - method name
      parameterTypes - parameter types the method has
      Returns:
      accessible method
    • getMethod

      protected Method getMethod(Class target, String methodName, Class<?>... parameterTypes)
      Get method with given name and parameters in the given class.
      Parameters:
      target - class to get method from
      methodName - method name
      parameterTypes - parameter types the method has
      Returns:
      accessible method
    • fireDomEvent

      protected void fireDomEvent(String eventType)
      Fires a DOM event of the given type on the wrapped component.
      Parameters:
      eventType - the type of the event, not null.
    • fireDomEvent

      protected void fireDomEvent(String eventType, elemental.json.JsonObject eventData)
      Fires a DOM event with the given type and payload on the wrapped component.
      Parameters:
      eventType - the type of the event, not null.
      eventData - additional data related to the event, not null
    • fireDomEvent

      protected void fireDomEvent(DomEvent event)
      Fires a DOM event on the wrapped component.
      Parameters:
      event - the event that should be fired.
    • findByQuery

      protected <R extends Component> Optional<R> findByQuery(Class<R> componentType, Consumer<ComponentQuery<R>> queryBuilder)
      Searches for a nested component of the given type that matches the conditions set on the component query. Query is expected to return zero or one component. An exception is thrown if more than one component matches the specifications. Usually the ComponentQuery consumer should only define conditions, not invoke any terminal operator.
      Type Parameters:
      R - the type of the component to search for
      Parameters:
      componentType - the type of the component to search for
      queryBuilder - the function that sets query condition
      Returns:
      the component found by query execution, wrapped into an Optional, or empty if the query does not produce results.
    • findAllByQuery

      protected <R extends Component> List<R> findAllByQuery(Class<R> componentType, Consumer<ComponentQuery<R>> queryBuilder)
      Searches for nested components of the given type that matches the conditions set on the component query. Usually the ComponentQuery consumer should only define conditions, not invoke any terminal operator.
      Type Parameters:
      R - the type of the component to search for
      Parameters:
      componentType - the type of the component to search for
      queryBuilder - the function that sets query condition
      Returns:
      the components found by query execution, or an empty list.