Class GridContextMenuTester<T extends GridContextMenu<Y>,Y>

java.lang.Object
com.vaadin.browserless.ComponentTester<T>
com.vaadin.flow.component.grid.contextmenu.GridContextMenuTester<T,Y>
Type Parameters:
T - component type
Y - item type
All Implemented Interfaces:
Clickable<T>

public class GridContextMenuTester<T extends GridContextMenu<Y>,Y> extends ComponentTester<T>
Tester for GridContextMenu components.

A grid context menu is always about a row: the user right-clicks a row, and the events the menu fires report that row. open(int) therefore takes the row to open the menu on, addressed by its zero-based index among the rows the user sees, the same way GridTester addresses rows.

The menu content is not part of the UI until the menu opens, so clickItem(...) and find(Class) only see the items of an open menu.

  • Constructor Details

    • GridContextMenuTester

      public GridContextMenuTester(T component)
      Wrap grid context menu for testing.

      The menu targets no row, so it has to be opened with open(int).

      Parameters:
      component - target grid context menu
    • GridContextMenuTester

      public GridContextMenuTester(T component, int row)
      Wrap grid context menu for testing, targeting the given row.

      The index is 0 based and counts the rows the user sees. Opening the menu with open() opens it on that row.

      Parameters:
      component - target grid context menu
      row - row the menu is about
  • Method Details

    • open

      public void open()
      Opens the context menu on the row it currently targets, as if the user had asked for it in the browser.

      The target row is the one given to GridTester.contextMenu(int), so this method is the counterpart of that entry point. Use open(int) to open the menu on a row directly.

      It does not render any client-side overlay, it only simulates the server-side state changes that opening the menu produces: the menu is attached to the UI, so its items become findable and clickable, and a GridContextMenuOpenedEvent reporting the target row is fired.

      Throws:
      IllegalStateException - if the menu does not target a row, if it is already open, or if a dynamic content handler prevented it from opening
    • open

      public void open(int row)
      Opens the context menu on the given row, as if the user had asked for it in the browser.

      The index is 0 based and counts the rows the user sees. The row is reported by the events the menu fires, so GridContextMenuItemClickEvent.getItem() and GridContextMenuOpenedEvent.getItem() hold the item on that row.

      Parameters:
      row - row to open the menu on
      Throws:
      IllegalStateException - if the menu is already open, or if a dynamic content handler prevented it from opening
      IndexOutOfBoundsException - if the grid has no such row
    • open

      public void open(int row, String columnKey)
      Opens the context menu on the given row and column, as if the user had asked for it in the browser.

      The column is reported by GridContextMenuOpenedEvent.getColumnId().

      Parameters:
      row - row to open the menu on, 0 based
      columnKey - key of the column to open the menu on, as set with Grid.Column.setKey(String)
      Throws:
      IllegalArgumentException - if the grid has no column with the given key
      IllegalStateException - if the column is not visible, if the menu is already open, or if a dynamic content handler prevented it from opening
      IndexOutOfBoundsException - if the grid has no such row
    • close

      public void close()
      Closes the context menu.
      Throws:
      IllegalStateException - if the menu is not open
    • clickItem

      public void clickItem(String topLevelText, String... nestedItemsText)
      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, an IllegalStateException is thrown because the target is ambiguous. Disabled or invisible items cannot be clicked and will also cause an IllegalStateException.

      The menu has to be open, since its items are not part of the UI before that.

      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 open, 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 an IllegalStateException.

      The menu has to be open, since its items are not part of the UI before that.

      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 open, or if the item at the given position is disabled or not visible.
    • isItemChecked

      public boolean isItemChecked(String topLevelText, String... nestedItemsText)
      Checks if the checkable menu item matching given text is checked.

      For a nested menu item, provide the text of each menu item in the hierarchy.

      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 open, 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 a nested menu item, provide the position of each sub menu that should be navigated to reach the requested item.

      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:
      true if the item at given position is checked, otherwise false.
      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 open, or if the item at given position is not usable.
    • getItemTooltipText

      public String getItemTooltipText(String topLevelText, String... nestedItemsText)
      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.

      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 open, or if the item at given path is not usable.
    • getItemTooltipText

      public String getItemTooltipText(int topLevelPosition, int... nestedItemsPositions)
      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.

      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 open, or if the item at given position is not usable.
    • getItemTexts

      public List<String> 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 to clickItem(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. The items are the ones the menu offers for the row it was opened on, so a dynamic content handler has run by then.

       
      
       menu.addItem("Edit", event -> {
       });
       menu.addItem("Hidden", event -> {
       }).setVisible(false);
       menu.addItem("Share");
      
       tester.open(0);
      
       // ["Edit", "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

      public List<String> 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.

      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 -> {
       });
      
       tester.open(0);
      
       // ["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

      public <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 ComponentRenderer column 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 then clickItem(...) for a context menu, and GridTester.contextMenu(row).open() for a grid context menu.

      Can be used to find components in the context menu. The menu content is only part of the UI while the menu is open, so components are returned in a detached state unless open(int) has been called previously.

       // view:
       GridContextMenu<Person> menu = grid.addContextMenu();
       menu.addItem(new Checkbox("Show inactive"), event -> {
       });
      
       // test:
       GridContextMenuTester<GridContextMenu<Person>, Person> menuTester = test(
               view.menu);
       menuTester.open(0);
       Checkbox checkbox = menuTester.find(Checkbox.class).single();
       
      Overrides:
      find in class ComponentTester<T extends GridContextMenu<Y>>
      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.