Class ContextMenuTester

  • All Implemented Interfaces:
    com.vaadin.browserless.Clickable

    
    public class ContextMenuTester<T extends ContextMenu>
    extends ComponentTester<T>
                        

    Tester for ContextMenu components.

    Since:

    1.0

    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
    • Field Summary

      Fields 
      Modifier and Type Field Description
      public final T component
    • Constructor Summary

      Constructors 
      Constructor Description
      ContextMenuTester(T component) Wrap given component for testing.
    • Enum Constant Summary

      Enum Constants 
      Enum Constant Description
    • Method Summary

      Modifier and Type Method Description
      void open() Opens the context menu, as if the action is done in the browser.
      void close() Closes the context menu.
      void clickItem(String topLevelText, Array<String> nestedItemsText) Simulates a click on the item that matches the given text.
      void clickItem(int topLevelPosition, Array<int> nestedItemsPositions) Simulates a click on the item at the given position in the menu.
      boolean isItemChecked(String topLevelText, Array<String> nestedItemsText) Checks if the checkable menu item matching given text is checked.
      boolean isItemChecked(int topLevelPosition, Array<int> nestedItemsPositions) Checks if the checkable menu item at given position is checked.
      String getItemTooltipText(String topLevelText, Array<String> nestedItemsText) Gets the tooltip text of the menu item matching the given text.
      String getItemTooltipText(int topLevelPosition, Array<int> nestedItemsPositions) Gets the tooltip text of the menu item at the given position in the menu.
      <R extends Component> ComponentQuery<R> find(Class<R> componentType) Gets a to search for component of the given type nested inside the wrapped component.
      • Methods inherited from class com.vaadin.browserless.ComponentTester

        ensureComponentIsUsable, getComponent, isUsable, setModal
      • Methods inherited from class com.vaadin.browserless.Clickable

        click, click, click, middleClick, middleClick, rightClick, rightClick
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • ContextMenuTester

        ContextMenuTester(T component)

        Wrap given component for testing.

        Parameters:
        component - target component
    • Method Detail

      • open

         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.

        Calling open is not required before using clickItem(...) or find(Class) methods. Those methods operate on the server component state and can be used regardless of the menu opened state. Use open only if you want to explicitly simulate the act of opening.

      • close

         void close()

        Closes the context menu.

      • clickItem

         void clickItem(String topLevelText, Array<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.

        
        
        menu.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");
        
        Note: Opening the menu via open is not required before invoking this method; the lookup operates on server-side state.
        Parameters:
        topLevelText - the text content of the top level menu item, not null.
        nestedItemsText - text content of the nested menu items
      • clickItem

         void clickItem(int topLevelPosition, Array<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.

        
        
        menu.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);
        
        Note: Opening the menu via open is not required before invoking this method; the lookup operates on server-side state.
        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
      • isItemChecked

         boolean isItemChecked(String topLevelText, Array<String> nestedItemsText)

        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.

      • isItemChecked

         boolean isItemChecked(int topLevelPosition, Array<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
      • getItemTooltipText

         String getItemTooltipText(String topLevelText, Array<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. 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.

        Since:

        1.1

      • getItemTooltipText

         String getItemTooltipText(int topLevelPosition, Array<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. 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.

        Since:

        1.1

      • find

         <R extends Component> ComponentQuery<R> find(Class<R> componentType)

        Gets a to search for component of the given type nested inside the wrapped component.

        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());