Package com.vaadin.testbench.unit
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,RouterLinkTester,SelectTester,SideNavTester,TabSheetTester,TabsTester,TextAreaTester,TextFieldTester,TimePickerTester,UploadTester,VirtualListTester
Test wrapper for components with helpful methods for testing a component.
More targeted methods for specific components exist in named component wrappers.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected final voidChecks that wrapped component is usable, otherwise throws anIllegalStateExceptionwith details on the current state of the component.protected static voidensureComponentIsUsable(Component component, Predicate<Component> usableTest) Throws anIllegalStateExceptionwith details on the current state of the component if it is not usable according to the provided test.protected voidCheck that the component is visible for the user.protected static voidensureVisible(Component component) Check that the given component is visible for the user.<R extends Component>
ComponentQuery<R> Gets aComponentQueryto search for component of the given type nested inside the wrapped component.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.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.protected voidfireDomEvent(DomEvent event) Fires a DOM event on the wrapped component.protected voidfireDomEvent(String eventType) Fires a DOM event of the given type on the wrapped component.protected voidfireDomEvent(String eventType, elemental.json.JsonObject eventData) Fires a DOM event with the given type and payload on the wrapped component.Get the wrapped component.protected FieldGet field with given name in the given class.protected FieldGet field with given name in the wrapped component.protected MethodGet method with given name and parameters in the given class.protected MethodGet method with given name and parameters in the wrapped component.booleanisUsable()Validate that component can be interacted with and should be visible in the UI.protected static booleanValidate that the given component can be interacted with and should be visible in the UI.protected static voidnotUsableReasons(Component component, Consumer<String> collector) Provides messages explaining why the given component is actually not usable.protected voidnotUsableReasons(Consumer<String> collector) Provides messages explaining why the component is actually not usable.protected voidSimulates a server round-trip, flushing pending component changes.voidsetModal(boolean modal) Set component modality.protected <V> voidsetValueAsUser(V value) Sets the value to given component.
-
Constructor Details
-
ComponentTester
Wrap given component for testing.- Parameters:
component- target component
-
-
Method Details
-
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 overridenotUsableReasons(Consumer)to provide additional details to the potential exception thrown byensureComponentIsUsable().- Returns:
trueif component can be interacted with by the user- See Also:
-
isUsable
Validate that the given component can be interacted with and should be visible in the UI. Subclasses overriding this method should also overridenotUsableReasons(Consumer)to provide additional details to the potential exception thrown byensureComponentIsUsable().- Returns:
trueif 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-trueto make component modal,falseto remove modality
-
find
Gets aComponentQueryto 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
ComponentQueryinstance, searching for wrapped component children.
-
ensureComponentIsUsable
protected final void ensureComponentIsUsable()Checks that wrapped component is usable, otherwise throws anIllegalStateExceptionwith details on the current state of the component. -
ensureComponentIsUsable
Throws anIllegalStateExceptionwith details on the current state of the component if it is not usable according to the provided test.- Parameters:
component- the component to checkusableTest- function that tests if the component is usable or not.
-
notUsableReasons
Provides messages explaining why the component is actually not usable. Subclasses overridingisUsable()should also override this method to provide additional details to the potential exception throw byensureComponentIsUsable().- See Also:
-
notUsableReasons
Provides messages explaining why the given component is actually not usable. Subclasses overridingisUsable()should also override this method to provide additional details to the potential exception throw byensureComponentIsUsable().- See Also:
-
ensureVisible
protected void ensureVisible()Check that the component is visible for the user. Else throw anIllegalStateException -
ensureVisible
Check that the given component is visible for the user. Else throw anIllegalStateException -
roundTrip
protected void roundTrip()Simulates a server round-trip, flushing pending component changes. -
getField
Get field with given name in the wrapped component.- Parameters:
fieldName- field name- Returns:
- accessible field
- Throws:
IllegalArgumentException- if field doesn't exist
-
getField
Get field with given name in the given class.- Parameters:
target- class to get field fromfieldName- field name- Returns:
- accessible field
- Throws:
IllegalArgumentException- if field doesn't exist
-
getMethod
Get method with given name and parameters in the wrapped component.- Parameters:
methodName- method nameparameterTypes- parameter types the method has- Returns:
- accessible method
-
getMethod
Get method with given name and parameters in the given class.- Parameters:
target- class to get method frommethodName- method nameparameterTypes- parameter types the method has- Returns:
- accessible method
-
fireDomEvent
Fires a DOM event of the given type on the wrapped component.- Parameters:
eventType- the type of the event, not null.
-
fireDomEvent
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
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 theComponentQueryconsumer 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 forqueryBuilder- 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 theComponentQueryconsumer 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 forqueryBuilder- the function that sets query condition- Returns:
- the components found by query execution, or an empty list.
-
setValueAsUser
protected <V> void setValueAsUser(V value) Sets the value to given component. Supports pretending that the value came from the browser. Will throw an exception if the component is not instance of AbstractField. This method is purposed for internal use and when creating custom testers extending ComponentTesters.- Parameters:
value- the new value, may be null.
-