Class ComponentTester
-
- All Implemented Interfaces:
-
com.vaadin.browserless.Clickable
public class ComponentTester<T extends Component> implements Clickable<T>
Test wrapper for components with helpful methods for testing a component.
More targeted methods for specific components exist in named component wrappers.
- Since:
1.0
-
-
Constructor Summary
Constructors Constructor Description ComponentTester(T component)Wrap given component for testing.
-
Method Summary
Modifier and Type Method Description TgetComponent()Get the wrapped component. booleanisUsable()Validate that component can be interacted with and should be visible in the UI. voidsetModal(boolean modal)Set component modality. <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. final voidensureComponentIsUsable()Checks that wrapped component is usable, otherwise throws an IllegalStateException with details on the current state of the component. voidfocus()Simulates the user moving keyboard focus to the wrapped component. booleanisFocused()Checks whether the wrapped component currently has keyboard focus in the simulated browser, including focus given with server-side focus calls. voidblur()Simulates the wrapped component losing keyboard focus, firing a blur event as if it came from the client. -
-
Constructor Detail
-
ComponentTester
ComponentTester(T component)
Wrap given component for testing.
- Parameters:
component- target component
-
-
Method Detail
-
getComponent
T getComponent()
Get the wrapped component.
- Returns:
wrapped component
-
isUsable
boolean isUsable()
Validate that component can be interacted with and should be visible in the UI. Subclasses overriding this method should also override notUsableReasons to provide additional details to the potential exception thrown by ensureComponentIsUsable.
- Returns:
trueif component can be interacted with by the user
-
setModal
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
<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.
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.- Parameters:
componentType- type of the component to search.- Returns:
a ComponentQuery instance, searching for wrapped component children.
-
ensureComponentIsUsable
final void ensureComponentIsUsable()
Checks that wrapped component is usable, otherwise throws an IllegalStateException with details on the current state of the component.
-
focus
void focus()
Simulates the user moving keyboard focus to the wrapped component.
Fires a blur event on the previously focused component and a focus event on this one, as if they came from the client. Focus also moves implicitly when interacting with components through testers, so calling this is rarely needed.
-
isFocused
boolean isFocused()
Checks whether the wrapped component currently has keyboard focus in the simulated browser, including focus given with server-side focus calls.
- Returns:
trueif the component is focused
-
blur
void blur()
Simulates the wrapped component losing keyboard focus, firing a blur event as if it came from the client. Blurring a component that does not have focus is a no-op, as in a browser.
Focus also moves implicitly when interacting with other components through testers, so calling this is only needed when nothing else is interacted with after this component.
-
-
-
-