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 void
Checks that wrapped component is usable, otherwise throws anIllegalStateException
with details on the current state of the component.protected static void
ensureComponentIsUsable
(Component component, Predicate<Component> usableTest) Throws anIllegalStateException
with details on the current state of the component if it is not usable according to the provided test.protected void
Check that the component is visible for the user.protected static void
ensureVisible
(Component component) Check that the given component is visible for the user.<R extends Component>
ComponentQuery<R>Gets aComponentQuery
to 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 void
fireDomEvent
(DomEvent event) Fires a DOM event on the wrapped component.protected void
fireDomEvent
(String eventType) Fires a DOM event of the given type on the wrapped component.protected void
fireDomEvent
(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 Field
Get field with given name in the given class.protected Field
Get field with given name in the wrapped component.protected Method
Get method with given name and parameters in the given class.protected Method
Get method with given name and parameters in the wrapped component.boolean
isUsable()
Validate that component can be interacted with and should be visible in the UI.protected static boolean
Validate that the given component can be interacted with and should be visible in the UI.protected static void
notUsableReasons
(Component component, Consumer<String> collector) Provides messages explaining why the given component is actually not usable.protected void
notUsableReasons
(Consumer<String> collector) Provides messages explaining why the component is actually not usable.protected void
Simulates a server round-trip, flushing pending component changes.void
setModal
(boolean modal) Set component modality.
-
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:
true
if 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:
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
Gets aComponentQuery
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 anIllegalStateException
with details on the current state of the component. -
ensureComponentIsUsable
Throws anIllegalStateException
with 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 theComponentQuery
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 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 theComponentQuery
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 forqueryBuilder
- the function that sets query condition- Returns:
- the components found by query execution, or an empty list.
-