Class ComponentTester<T extends Component>
- Type Parameters:
T- component type
- All Implemented Interfaces:
Clickable<T>
- Direct Known Subclasses:
AbstractLoginTester,AccordionTester,AvatarGroupTester,BreadcrumbsTester,ButtonTester,CardTester,ChartTester,CheckboxGroupTester,CheckboxTester,ComboBoxTester,ConfirmDialogTester,ContextMenuTester,DashboardTester,DatePickerTester,DateTimePickerTester,DecimalRangeSliderTester,DecimalSliderTester,DetailsTester,DialogTester,GridContextMenuTester,GridTester,HtmlComponentTester,InputTester,IntegerRangeSliderTester,IntegerSliderTester,ListBoxTester,MarkdownTester,MasterDetailLayoutTester,MenuBarTester,MessageInputTester,MessageListTester,MultiSelectComboBoxTester,MultiSelectListBoxTester,NotificationTester,NumberFieldTester,PopoverTester,RadioButtonGroupTester,RadioButtonTester,RangeInputTester,RouterLinkTester,SelectTester,SideNavTester,SplitLayoutTester,SwitchTester,TabSheetTester,TabsTester,TextAreaTester,TextFieldTester,TextTester,TimePickerTester,UploadTester,VirtualListTester
More targeted methods for specific components exist in named component wrappers.
- Since:
- 1.0
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected booleancanSetValueAsUser(HasValue<?, ?> field) Checks whethersetValueAsUser(HasValue, Object)can simulate a client side value change on the given field.protected voidEmpties the field as the user would, by setting the component's empty value.protected voidEmpties the field by clicking its clear button, as the user would.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 voidChecks that the wrapped component is usable and, if it is not, detaches it from the UI before rethrowing.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, tools.jackson.databind.node.ObjectNode 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 or in one of its superclasses.protected FieldGet field with given name in the wrapped component.protected MethodGet method with given name and parameters in the given class or in one of its superclasses.protected MethodGet method with given name and parameters in the wrapped component.protected booleanChecks whether the wrapped component is a value component that is currently in read-only state.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 voidsetPropertyAsUser(String property, Serializable value) Updates an element property of the wrapped component as if the update was sent by the browser, so that events derived from the property change reportisFromClient() == true.protected <V> voidsetValueAsUser(HasValue<?, V> field, V value) Sets the value to the given field, pretending that the value came from the browser, so that the fired value change event reportsisFromClient() == true.protected <V> voidsetValueAsUser(V value) Sets the value to given component.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface com.vaadin.browserless.Clickable
click, click, click, middleClick, middleClick, rightClick, rightClick
-
Constructor Details
-
ComponentTester
Wrap given component for testing.- Parameters:
component- target component
-
-
Method Details
-
getComponent
Get the wrapped component.- Specified by:
getComponentin interfaceClickable<T extends 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:
-
isComponentReadOnly
protected boolean isComponentReadOnly()Checks whether the wrapped component is a value component that is currently in read-only state.A read-only
HasValuecomponent cannot be interacted with to change its value, so it is considered not usable.- Returns:
trueif the component is read-only
-
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.The query walks the server-side component tree. A component that another component renders per item, such as the component a
ComponentRenderercolumn renders for a grid row, is rendered into the column and not into the tree, and the content of an overlay, such as a context menu, is attached only while the overlay is open. Neither is reachable this way, and the lookup returns an empty result rather than failing, so reach those components through the owning component tester instead:GridTester.getCellComponent(row, column)for grid cells,ContextMenuTester.open()and thenclickItem(...)for a context menu, andGridTester.contextMenu(row).open()for a grid context menu.- 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
public final void ensureComponentIsUsable()Checks that wrapped component is usable, otherwise throws anIllegalStateExceptionwith details on the current state of the component.- Specified by:
ensureComponentIsUsablein interfaceClickable<T extends Component>
-
ensureComponentIsUsableOrDetach
protected void ensureComponentIsUsableOrDetach()Checks that the wrapped component is usable and, if it is not, detaches it from the UI before rethrowing.For an overlay the tester has to attach to the UI before it can tell whether it is usable, such as a context menu opened by a before-open event. Detaching it again keeps a refused interaction from leaving a closed overlay behind in the UI tree, where a top level
find(...)would still reach its content.- Throws:
IllegalStateException- if the component is not usable, with details on its current state.
-
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.The wrapped component is often an application's own subclass of the component the tester targets, so the field is looked up on the whole class hierarchy, not only on the component's concrete class.
- Parameters:
fieldName- field name- Returns:
- accessible field
- Throws:
IllegalArgumentException- if field doesn't exist
-
getField
Get field with given name in the given class or in one of its superclasses.- 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.The wrapped component is often an application's own subclass of the component the tester targets, so the method is looked up on the whole class hierarchy, not only on the component's concrete class.
- Parameters:
methodName- method nameparameterTypes- parameter types the method has- Returns:
- accessible method
-
getMethod
Get method with given name and parameters in the given class or in one of its superclasses.- 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.
-
clearAsUser
protected void clearAsUser()Empties the field as the user would, by setting the component's empty value.Emptying a field is always available to the user ? select the contents, press Delete ? and stays legal even when it leaves the field invalid, so the empty value is set unconditionally. This is the shared implementation behind the
clear()methods of the value testers; each of them declaresclear()itself so that the generated locators pick it up.- Throws:
IllegalStateException- if the component is not usableIllegalArgumentException- if the component does not hold a value
-
clickClearButtonAsUser
protected void clickClearButtonAsUser()Empties the field by clicking its clear button, as the user would.Unlike
clearAsUser(), which models the keyboard route and is therefore always available, this requires the clear button to actually be on screen: a hidden clear button is not something the user can click. Past that check the value is emptied exactly asclearAsUser()does.Testers for components implementing
HasClearButtonexpose this as a publicclickClearButton();LocatorProcessorfails the build when one of them does not.- Throws:
IllegalStateException- if the component is not usable, or its clear button is not visibleIllegalArgumentException- if the component does not hold a value
-
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.
-
setValueAsUser
Sets the value to the given field, pretending that the value came from the browser, so that the fired value change event reportsisFromClient() == true. Will throw an exception if the field is not backed by anAbstractFieldor anAbstractCompositeField; usecanSetValueAsUser(HasValue)to check up front.This method is purposed for internal use and when creating custom testers extending ComponentTesters, for fields other than the wrapped component, such as an editor field owned by the wrapped component.
- Parameters:
field- the field to set the value to, not null.value- the new value, may be null.
-
canSetValueAsUser
Checks whethersetValueAsUser(HasValue, Object)can simulate a client side value change on the given field. Only fields backed byAbstractFieldorAbstractCompositeFieldhave such a path; a tester driving a foreignHasValueimplementation has to fall back toHasValue.setValue(Object).- Parameters:
field- the field to check, not null.- Returns:
- true if the value can be set as a user
-
setPropertyAsUser
Updates an element property of the wrapped component as if the update was sent by the browser, so that events derived from the property change reportisFromClient() == true. Ends with aroundTrip().Use this for components that expose state as a synchronized element property rather than as a field value, such as the
openedproperty of Details and Accordion.- Parameters:
property- name of the property to update, not null.value- the value as the client would send it, may be null.- Throws:
IllegalStateException- if the property does not accept updates from the client
-