Class ContextMenuTester<T extends ContextMenu>
- Type Parameters:
T- component type
- All Implemented Interfaces:
Clickable<T>
- Since:
- 1.0
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidclickItem(int topLevelPosition, int... nestedItemsPositions) Simulates a click on the item at the given position in the menu.voidSimulates a click on the item that matches the given text.voidclose()Closes the context menu.<R extends Component>
ComponentQuery<R> Gets aComponentQueryto search for component of the given type nested inside the wrapped component.Gets the texts of the menu items, as the browser shows them.getItemTexts(String topLevelText, String... nestedItemsText) Gets the texts of the items of the sub menu of the item matching the given text, as the browser shows them.getItemTooltipText(int topLevelPosition, int... nestedItemsPositions) Gets the tooltip text of the menu item at the given position in the menu.getItemTooltipText(String topLevelText, String... nestedItemsText) Gets the tooltip text of the menu item matching the given text.booleanisItemChecked(int topLevelPosition, int... nestedItemsPositions) Checks if the checkable menu item at given position is checked.booleanisItemChecked(String topLevelText, String... nestedItemsText) Checks if the checkable menu item matching given text is checked.voidopen()Opens the context menu, as if the action is done in the browser.Methods inherited from class com.vaadin.browserless.ComponentTester
canSetValueAsUser, clearAsUser, clickClearButtonAsUser, ensureComponentIsUsable, ensureComponentIsUsable, ensureComponentIsUsableOrDetach, ensureVisible, ensureVisible, findAllByQuery, findByQuery, fireDomEvent, fireDomEvent, fireDomEvent, getComponent, getField, getField, getMethod, getMethod, isComponentReadOnly, isUsable, isUsable, notUsableReasons, notUsableReasons, roundTrip, setModal, setPropertyAsUser, setValueAsUser, setValueAsUserMethods 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
-
ContextMenuTester
Wrap given component for testing.- Parameters:
component- target component
-
-
Method Details
-
open
public void open()Opens the context menu, as if the action is done in the browser.It simulates, for example, a right click on a UI component with an assigned
ContextMenu.It does not render any client-side overlay. In other words, it only simulates the server-side state changes that would occur when a user opens the menu in the browser.
A closed context menu is not attached to the UI, so, exactly as in the browser, its items cannot be interacted with: open the menu before calling
clickItem(...),isItemChecked(...)orgetItemTooltipText(...), otherwise they throw anIllegalStateException. Onlyfind(Class)works on a closed menu, since it queries the menu contents instead of the UI; the components it returns are detached until the menu is opened.A top level
find(...)on the UI is a different matter: the menu content is attached to the UI only while the menu is open, so it is found afteropen()and not before.- Throws:
IllegalStateException- if the menu is already opened.
-
close
public void close()Closes the context menu. -
clickItem
Simulates a click on the item that matches the given text. For a nested menu item, provide the text of each menu item in the hierarchy. The path to the menu item must reflect what is seen in the browser, meaning that hidden items are ignored. If there are multiple visible items at the same level with the same text, anIllegalStateExceptionis thrown because the target is ambiguous. Disabled or invisible items cannot be clicked and will also cause anIllegalStateException.
Note: the menu must be opened withmenu.addItem("Preview", event -> { }); var subMenu = menu.addItem("Share").getSubMenu(); subMenu.addItem("Copy link", event -> { }); subMenu.addItem("Email", event -> { }); // clicks top level menu item with text Preview tester.clickItem("Preview"); // clicks nested menu item with text Email tester.clickItem("Share", "Email");open()before an item can be clicked, since a closed menu is not attached to the UI.- Parameters:
topLevelText- the text content of the top level menu item, not null.nestedItemsText- text content of the nested menu items- Throws:
IllegalArgumentException- if the provided text does not identify a menu item.IllegalStateException- if the menu is not opened, if there are multiple visible matching items at any level, or if the item at the given path is disabled or not visible.
-
clickItem
public void clickItem(int topLevelPosition, int... nestedItemsPositions) Simulates a click on the item at the given position in the menu. For a nested menu item, provide the position of each sub menu that should be navigated to reach the requested item. Positions are zero-based and refer only to items that are visible at each menu level, i.e. hidden items are ignored (the same way as in the browser). Disabled or invisible items cannot be clicked and will cause anIllegalStateException.
Note: the menu must be opened withmenu.addItem("Preview", event -> { }); var subMenu = menu.addItem("Share").getSubMenu(); subMenu.addItem("Copy link", event -> { }); subMenu.addItem("Email", event -> { }); // clicks top level "Preview" menu item at position 0 tester.clickItem(0); // clicks then nested menu item at position 1 "Email" through the // item "Share" at position 1 tester.clickItem(1, 1);open()before an item can be clicked, since a closed menu is not attached to the UI.- Parameters:
topLevelPosition- the zero-based position of the item in the menu, as it will be seen in the browser.nestedItemsPositions- the zero-based position of the nested items, relative to the parent menu- Throws:
IllegalArgumentException- if the provided position does not identify a menu item.IllegalStateException- if the menu is not opened, or if the item at the given position is disabled or not visible.
-
isItemChecked
Checks if the checkable menu item matching given text is checked. For nested menu item provide the text of each menu item in the hierarchy. The path to the menu item must reflect what is seen in the browser, meaning that hidden items are ignored.menu.addItem("Preview", event -> { }).setCheckable(true); var subMenu = menu.addItem("Share").getSubMenu(); subMenu.addItem("Copy link", event -> { }).setCheckable(true); subMenu.addItem("Email", event -> { }).setCheckable(true); tester.isItemChecked("Preview"); tester.isItemChecked("Share", "Email");- Parameters:
topLevelText- the text content of the top level menu item, not null.nestedItemsText- text content of the nested menu items- Returns:
- true if the item at given path is checked, otherwise false.
- Throws:
IllegalArgumentException- if the provided text does not identify a menu item or if the menu item is not checkable.IllegalStateException- if the menu is not opened, or if the item at given path is not usable.
-
isItemChecked
public boolean isItemChecked(int topLevelPosition, int... nestedItemsPositions) Checks if the checkable menu item at given position is checked. For nested menu item provide the position of each sub menu that should be navigated to reach the requested item. The position reflects what is seen in the browser, so hidden items are ignored.menu.addItem("Preview", event -> { }).setCheckable(true); var subMenu = menu.addItem("Share").getSubMenu(); subMenu.addItem("Copy link", event -> { }).setCheckable(true); subMenu.addItem("Email", event -> { }).setCheckable(true); // checks top level "Preview" menu item at position 0 tester.isItemChecked(0); // checks nested menu item at position 1 "Email" through the // item "Share" at position 1 tester.isItemChecked(1, 1);- Parameters:
topLevelPosition- the zero-based position of the item in the menu, as it will be seen in the browser.nestedItemsPositions- the zero-based position of the nested items, relative to the parent menu- Throws:
IllegalArgumentException- if the provided position does not identify a menu item or if the menu item is not checkable.IllegalStateException- if the menu is not opened, or if the item at given position is not usable.
-
getItemTooltipText
Gets the tooltip text of the menu item matching the given text. For a nested menu item, provide the text of each menu item in the hierarchy. The path to the menu item must reflect what is seen in the browser, meaning that hidden items are ignored.menu.addItem("Preview", event -> { }).setTooltipText("Preview the document"); var subMenu = menu.addItem("Share").getSubMenu(); subMenu.addItem("Email", event -> { }).setTooltipText("Send as email"); tester.getItemTooltipText("Preview"); tester.getItemTooltipText("Share", "Email");- Parameters:
topLevelText- the text content of the top level menu item, not null.nestedItemsText- text content of the nested menu items- Returns:
- the tooltip text of the menu item at given path, or null if the item has no tooltip set.
- Throws:
IllegalArgumentException- if the provided text does not identify a menu item.IllegalStateException- if the menu is not opened, or if the item at given path is not usable.- Since:
- 1.1
-
getItemTooltipText
Gets the tooltip text of the menu item at the given position in the menu. For a nested menu item, provide the position of each sub menu that should be navigated to reach the requested item. The position reflects what is seen in the browser, so hidden items are ignored.menu.addItem("Preview", event -> { }).setTooltipText("Preview the document"); var subMenu = menu.addItem("Share").getSubMenu(); subMenu.addItem("Email", event -> { }).setTooltipText("Send as email"); // gets the tooltip of the top level "Preview" item at position 0 tester.getItemTooltipText(0); // gets the tooltip of the nested "Email" item at position 0 through // the item "Share" at position 1 tester.getItemTooltipText(1, 0);- Parameters:
topLevelPosition- the zero-based position of the item in the menu, as it will be seen in the browser.nestedItemsPositions- the zero-based position of the nested items, relative to the parent menu- Returns:
- the tooltip text of the menu item at given position, or null if the item has no tooltip set.
- Throws:
IllegalArgumentException- if the provided position does not identify a menu item.IllegalStateException- if the menu is not opened, or if the item at given position is not usable.- Since:
- 1.1
-
getItemTexts
Gets the texts of the menu items, as the browser shows them.Hidden items are ignored, so the returned texts are aligned with the positions used by
clickItem(int, int...). A text can also be given toclickItem(String, String...), as long as it identifies a single enabled item: a text that several visible items share is ambiguous, and a disabled item cannot be clicked.An item created from a component has no text of its own, and is reported as an empty string. Use
find(Class)to reach such an item.The menu has to be open, since its items are not part of the UI before that.
menu.addItem("Preview", event -> { }); menu.addItem("Hidden", event -> { }).setVisible(false); menu.addItem("Share"); // ["Preview", "Share"] tester.getItemTexts();- Returns:
- the texts of the visible top level menu items, in the order they are shown in
- Throws:
IllegalStateException- if the menu is not open, or is not visible
-
getItemTexts
Gets the texts of the items of the sub menu of the item matching the given text, as the browser shows them.For a nested sub menu, provide the text of each menu item in the hierarchy, the same way as in
clickItem(String, String...).Hidden items are ignored at every level, both when following the path and in the returned texts.
The menu has to be open, since its items are not part of the UI before that.
var subMenu = menu.addItem("Share").getSubMenu(); subMenu.addItem("Copy link", event -> { }); subMenu.addItem("Email", event -> { }); // ["Copy link", "Email"] tester.getItemTexts("Share");- Parameters:
topLevelText- the text content of the top level menu item, not null.nestedItemsText- text content of the nested menu items- Returns:
- the texts of the visible items of the sub menu, in the order they are shown in
- Throws:
IllegalArgumentException- if the provided text does not identify a menu item, or if the item at the given path has no sub menu.IllegalStateException- if the menu is not open or not visible, if there are multiple matching items at any level, or if the item at the given path is disabled or not visible.
-
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.Can be used to find components in the context menu. Returned components are in a detached state unless
open()has been called previously. Example usage:// view: ContextMenu menu = new ContextMenu(); menu.addItem(new VerticalLayout(new Div("Component Item")), click -> clickedItems.add("Component Item")); // test: ContextMenuTestermenuTester = test(view.menu); menuTester.open(); Div div = menuTester.find(Div.class).withText("Component Item").single(); Assertions.assertTrue(div.isAttached()); menuTester.close(); div = menuTester.find(Div.class).withText("Component Item").single(); Assertions.assertFalse(div.isAttached()); - Overrides:
findin classComponentTester<T extends ContextMenu>- 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.
-