From 309830a81b7d57084fc739685ededd9941ba766d Mon Sep 17 00:00:00 2001 From: Tatu Lund Date: Wed, 1 Mar 2023 14:38:44 +0200 Subject: [PATCH 1/8] Improve ContextMenuElement --- .../testbench/ContextMenuElement.java | 39 ++++++++++++++++--- 1 file changed, 33 insertions(+), 6 deletions(-) diff --git a/vaadin-context-menu-flow-parent/vaadin-context-menu-testbench/src/main/java/com/vaadin/flow/component/contextmenu/testbench/ContextMenuElement.java b/vaadin-context-menu-flow-parent/vaadin-context-menu-testbench/src/main/java/com/vaadin/flow/component/contextmenu/testbench/ContextMenuElement.java index 2c0545b587f..3b8b58e71c0 100644 --- a/vaadin-context-menu-flow-parent/vaadin-context-menu-testbench/src/main/java/com/vaadin/flow/component/contextmenu/testbench/ContextMenuElement.java +++ b/vaadin-context-menu-flow-parent/vaadin-context-menu-testbench/src/main/java/com/vaadin/flow/component/contextmenu/testbench/ContextMenuElement.java @@ -1,10 +1,5 @@ -package com.vaadin.flow.component.contextmenu.testbench; - -import com.vaadin.testbench.TestBenchElement; -import com.vaadin.testbench.elementsbase.Element; - /* - * Copyright 2000-2022 Vaadin Ltd. + * Copyright 2000-2023 Vaadin Ltd. * * Licensed under the Apache License, Version 2.0 (the "License"); you may not * use this file except in compliance with the License. You may obtain a copy of @@ -18,6 +13,13 @@ * License for the specific language governing permissions and limitations under * the License. */ +package com.vaadin.flow.component.contextmenu.testbench; + +import org.openqa.selenium.StaleElementReferenceException; +import org.openqa.selenium.interactions.Actions; + +import com.vaadin.testbench.TestBenchElement; +import com.vaadin.testbench.elementsbase.Element; /** * A TestBench element representing a <vaadin-context-menu> @@ -29,4 +31,29 @@ @Element("vaadin-context-menu") public class ContextMenuElement extends TestBenchElement { + /** + * This is an utility method, which will produce context click on the target + * element. If the target had ContextMenu, after opening the last + * ContextMenuOverlayElement can be used to find its menu items. + * + * @param target + * The element to which the ContextMenu has been hooked to. + */ + public static void openByRightClick(TestBenchElement target) { + Actions action = new Actions(target.getDriver()); + action.contextClick(target).perform(); + } + + /** + * Check if the ContextMenu is open. + * + * @return boolean True if menu is open. + */ + public boolean isOpen() { + try { + return getAttribute("opened").equals("true"); + } catch (StaleElementReferenceException e) { + return false; + } + } } From ec44da6326679eea84bf1447d6c875d9bc75a426 Mon Sep 17 00:00:00 2001 From: Tatu Lund Date: Wed, 1 Mar 2023 14:41:11 +0200 Subject: [PATCH 2/8] Add ContextMenuOverlayElement --- .../testbench/ContextMenuOverlayElement.java | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 vaadin-context-menu-flow-parent/vaadin-context-menu-testbench/src/main/java/com/vaadin/flow/component/contextmenu/testbench/ContextMenuOverlayElement.java diff --git a/vaadin-context-menu-flow-parent/vaadin-context-menu-testbench/src/main/java/com/vaadin/flow/component/contextmenu/testbench/ContextMenuOverlayElement.java b/vaadin-context-menu-flow-parent/vaadin-context-menu-testbench/src/main/java/com/vaadin/flow/component/contextmenu/testbench/ContextMenuOverlayElement.java new file mode 100644 index 00000000000..45c9ced4391 --- /dev/null +++ b/vaadin-context-menu-flow-parent/vaadin-context-menu-testbench/src/main/java/com/vaadin/flow/component/contextmenu/testbench/ContextMenuOverlayElement.java @@ -0,0 +1,52 @@ +/* + * Copyright 2000-2023 Vaadin Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.vaadin.flow.component.contextmenu.testbench; + +import java.util.List; +import java.util.Optional; + +import com.vaadin.testbench.TestBenchElement; +import com.vaadin.testbench.elementsbase.Element; + +/** + * A TestBench element representing a + * <vaadin-context-menu-overlay> element. + * + * @author Vaadin Ltd + * + */ +@Element("vaadin-context-menu-overlay") +public class ContextMenuOverlayElement extends TestBenchElement { + + /** + * Get the first menu item matching the caption. + * + * @return Optional menu item. + */ + public Optional getMenuItem(String caption) { + return getMenuItems().stream() + .filter(item -> item.getText().equals(caption)).findFirst(); + } + + /** + * Get the MenuItems of this ContextMenuOverlayElement. + * + * @return List of ContextMenuItemElement. + */ + public List getMenuItems() { + return $(ContextMenuItemElement.class).all(); + } +} From 03522d731b1337f84799f20aa2df58741cc2fa30 Mon Sep 17 00:00:00 2001 From: Tatu Lund Date: Wed, 1 Mar 2023 14:42:15 +0200 Subject: [PATCH 3/8] Add ContextMenuItemElement --- .../testbench/ContextMenuItemElement.java | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 vaadin-context-menu-flow-parent/vaadin-context-menu-testbench/src/main/java/com/vaadin/flow/component/contextmenu/testbench/ContextMenuItemElement.java diff --git a/vaadin-context-menu-flow-parent/vaadin-context-menu-testbench/src/main/java/com/vaadin/flow/component/contextmenu/testbench/ContextMenuItemElement.java b/vaadin-context-menu-flow-parent/vaadin-context-menu-testbench/src/main/java/com/vaadin/flow/component/contextmenu/testbench/ContextMenuItemElement.java new file mode 100644 index 00000000000..2182f3b6b81 --- /dev/null +++ b/vaadin-context-menu-flow-parent/vaadin-context-menu-testbench/src/main/java/com/vaadin/flow/component/contextmenu/testbench/ContextMenuItemElement.java @@ -0,0 +1,48 @@ +/* + * Copyright 2000-2023 Vaadin Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.vaadin.flow.component.contextmenu.testbench; + +import org.openqa.selenium.WebElement; +import org.openqa.selenium.interactions.Actions; + +import com.vaadin.testbench.TestBenchElement; +import com.vaadin.testbench.elementsbase.Element; + +/** + * A TestBench element representing a <vaadin-context-menu> + * element. + * + * @author Vaadin Ltd + * + */ +@Element("vaadin-context-menu-item") +public class ContextMenuItemElement extends TestBenchElement { + + /** + * Open the potential sub menu of the this item by hovering. If there was a + * submenu, after opening the last ContextMenuOverlayElement can be used to + * find its menu items. + */ + public void openSubMenu() { + hoverOn(this); + } + + protected void hoverOn(WebElement element) { + Actions action = new Actions(getDriver()); + action.moveToElement(element).perform(); + } + +} From bad9597e00dcf72acc4051c4e94b19b4a9da7c86 Mon Sep 17 00:00:00 2001 From: Tatu Lund Date: Wed, 1 Mar 2023 14:43:43 +0200 Subject: [PATCH 4/8] Add integration test --- .../contextmenu/it/SubMenuHelpersIT.java | 95 +++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 vaadin-context-menu-flow-parent/vaadin-context-menu-flow-integration-tests/src/test/java/com/vaadin/flow/component/contextmenu/it/SubMenuHelpersIT.java diff --git a/vaadin-context-menu-flow-parent/vaadin-context-menu-flow-integration-tests/src/test/java/com/vaadin/flow/component/contextmenu/it/SubMenuHelpersIT.java b/vaadin-context-menu-flow-parent/vaadin-context-menu-flow-integration-tests/src/test/java/com/vaadin/flow/component/contextmenu/it/SubMenuHelpersIT.java new file mode 100644 index 00000000000..3a4f3cc6777 --- /dev/null +++ b/vaadin-context-menu-flow-parent/vaadin-context-menu-flow-integration-tests/src/test/java/com/vaadin/flow/component/contextmenu/it/SubMenuHelpersIT.java @@ -0,0 +1,95 @@ +/* + * Copyright 2000-2023 Vaadin Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.vaadin.flow.component.contextmenu.it; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import com.vaadin.flow.component.button.testbench.ButtonElement; +import com.vaadin.flow.component.contextmenu.testbench.ContextMenuElement; +import com.vaadin.flow.component.contextmenu.testbench.ContextMenuItemElement; +import com.vaadin.flow.component.contextmenu.testbench.ContextMenuOverlayElement; +import com.vaadin.flow.component.menubar.testbench.MenuBarElement; +import com.vaadin.flow.component.notification.testbench.NotificationElement; + +import com.vaadin.tests.AbstractComponentIT; +import com.vaadin.flow.testutil.TestPath; + +@TestPath("vaadin-context-menu/sub-menu-helpers-test") +public class SubMenuHelpersIT extends AbstractComponentIT { + + @Before + public void init() { + open(); + } + + @Test + public void menuBarSubMenuTest() { + MenuBarElement menuBar = $(MenuBarElement.class).first(); + menuBar.getButtons().get(0).click(); + Assert.assertEquals("Bar Item", + $(NotificationElement.class).last().getText()); + + menuBar.getButtons().get(1).click(); + ContextMenuOverlayElement overlay = $(ContextMenuOverlayElement.class) + .last(); + ContextMenuItemElement menuBarSubItem = overlay.getMenuItems().get(0); + Assert.assertEquals("Bar Sub Item", menuBarSubItem.getText()); + menuBarSubItem.click(); + Assert.assertEquals("Bar Sub Item", + $(NotificationElement.class).last().getText()); + + menuBar.getButtons().get(1).click(); + overlay = $(ContextMenuOverlayElement.class).last(); + overlay.getMenuItems().get(1).openSubMenu(); + ContextMenuOverlayElement subOverlay = $( + ContextMenuOverlayElement.class).last(); + subOverlay.getMenuItems().get(0).click(); + Assert.assertEquals("Bar Sub Sub Item", + $(NotificationElement.class).last().getText()); + } + + @Test + public void contextMenuSubMenuTest() { + ButtonElement button = $(ButtonElement.class).first(); + ContextMenuElement.openByRightClick(button); + ContextMenuOverlayElement overlay = $(ContextMenuOverlayElement.class) + .first(); + overlay.getMenuItem("Context Item").get().click(); + Assert.assertEquals("Context Item", + $(NotificationElement.class).last().getText()); + + ContextMenuElement.openByRightClick(button); + overlay = $(ContextMenuOverlayElement.class).first(); + ContextMenuItemElement itemToOpen = overlay + .getMenuItem("Context Sub Menu").get(); + itemToOpen.openSubMenu(); + $(ContextMenuOverlayElement.class).last().getMenuItems().get(0).click(); + Assert.assertEquals("Context Sub Item", + $(NotificationElement.class).last().getText()); + } + + @Test + public void contextMenuOpenCloseTest() { + ButtonElement button = $(ButtonElement.class).first(); + ContextMenuElement.openByRightClick(button); + ContextMenuElement contextMenu = $(ContextMenuElement.class).first(); + Assert.assertTrue(contextMenu.isOpen()); + button.click(); + Assert.assertFalse(contextMenu.isOpen()); + } +} From c1230c25443451a0473eeacd010cfad47a65f88b Mon Sep 17 00:00:00 2001 From: Tatu Lund Date: Wed, 1 Mar 2023 14:45:14 +0200 Subject: [PATCH 5/8] Add test view --- .../contextmenu/it/SubMenuHelpersPage.java | 71 +++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 vaadin-context-menu-flow-parent/vaadin-context-menu-flow-integration-tests/src/main/java/com/vaadin/flow/component/contextmenu/it/SubMenuHelpersPage.java diff --git a/vaadin-context-menu-flow-parent/vaadin-context-menu-flow-integration-tests/src/main/java/com/vaadin/flow/component/contextmenu/it/SubMenuHelpersPage.java b/vaadin-context-menu-flow-parent/vaadin-context-menu-flow-integration-tests/src/main/java/com/vaadin/flow/component/contextmenu/it/SubMenuHelpersPage.java new file mode 100644 index 00000000000..9c93841d125 --- /dev/null +++ b/vaadin-context-menu-flow-parent/vaadin-context-menu-flow-integration-tests/src/main/java/com/vaadin/flow/component/contextmenu/it/SubMenuHelpersPage.java @@ -0,0 +1,71 @@ +/* + * Copyright 2000-2023 Vaadin Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.vaadin.flow.component.contextmenu.it; + +import com.vaadin.flow.component.button.Button; +import com.vaadin.flow.component.contextmenu.ContextMenu; +import com.vaadin.flow.component.contextmenu.MenuItem; +import com.vaadin.flow.component.contextmenu.SubMenu; +import com.vaadin.flow.component.html.Div; +import com.vaadin.flow.component.icon.VaadinIcon; +import com.vaadin.flow.component.menubar.MenuBar; +import com.vaadin.flow.component.notification.Notification; +import com.vaadin.flow.router.Route; + +@Route("vaadin-context-menu/sub-menu-helpers-test") +public class SubMenuHelpersPage extends Div { + + public SubMenuHelpersPage() { + MenuBar menuBar = new MenuBar(); + MenuItem menuBarItem = menuBar.addItem("Bar Item", e -> { + Notification.show("Bar Item"); + }); + MenuItem menuBarSub = menuBar.addItem("Bar Sub Menu"); + SubMenu menuBarSubMenu = menuBarSub.getSubMenu(); + MenuItem menuBarSubItem = menuBarSubMenu.addItem("Bar Sub Item", e -> { + Notification.show("Bar Sub Item"); + }); + MenuItem menuBarSubSub = menuBarSubMenu.addItem("Bar Sub Sub Menu"); + SubMenu menuBarSubSubMenu = menuBarSubSub.getSubMenu(); + MenuItem menuBarSubSubItem = menuBarSubSubMenu + .addItem("Bar Sub Sub Item", e -> { + Notification.show("Bar Sub Sub Item"); + }); + + ContextMenu contextMenu = new ContextMenu(); + MenuItem contextMenuItem = contextMenu.addItem("Context Item", e -> { + Notification.show("Context Item"); + }); + MenuItem contextMenuSub = contextMenu.addItem("Context Sub Menu"); + SubMenu contextBarSubMenu = contextMenuSub.getSubMenu(); + MenuItem contextMenuSubItem = contextBarSubMenu + .addItem("Context Sub Item", e -> { + Notification.show("Context Sub Item"); + }); + MenuItem contextMenuSubSub = contextBarSubMenu + .addItem("Context Sub Sub Menu"); + SubMenu contextBarSubSubMenu = contextMenuSubSub.getSubMenu(); + MenuItem contextMenuSubSubItem = contextBarSubSubMenu + .addItem("Context Sub Sub Item", e -> { + Notification.show("Context Sub Sub Item"); + }); + + Button button = new Button(VaadinIcon.MENU.create()); + contextMenu.setTarget(button); + + add(menuBar, button); + } +} From 1a4fbaf2110312befa43442db8deded5b67633ee Mon Sep 17 00:00:00 2001 From: Tatu Lund Date: Wed, 1 Mar 2023 14:48:06 +0200 Subject: [PATCH 6/8] Add dependencies --- .../pom.xml | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/vaadin-context-menu-flow-parent/vaadin-context-menu-flow-integration-tests/pom.xml b/vaadin-context-menu-flow-parent/vaadin-context-menu-flow-integration-tests/pom.xml index 554dff22040..4798cdd5365 100644 --- a/vaadin-context-menu-flow-parent/vaadin-context-menu-flow-integration-tests/pom.xml +++ b/vaadin-context-menu-flow-parent/vaadin-context-menu-flow-integration-tests/pom.xml @@ -36,6 +36,21 @@ vaadin-checkbox-flow ${project.version} + + com.vaadin + vaadin-button-flow + ${project.version} + + + com.vaadin + vaadin-menu-bar-flow + ${project.version} + + + com.vaadin + vaadin-notification-flow + ${project.version} + com.vaadin vaadin-ordered-layout-flow @@ -60,6 +75,24 @@ ${project.version} test + + com.vaadin + vaadin-menu-bar-testbench + ${project.version} + test + + + com.vaadin + vaadin-button-testbench + ${project.version} + test + + + com.vaadin + vaadin-notification-testbench + ${project.version} + test + com.vaadin flow-html-components-testbench From 79ee1702ea1c80882ca1910a5a29de6028b8d9b9 Mon Sep 17 00:00:00 2001 From: Tatu Lund Date: Tue, 6 May 2025 12:46:50 +0300 Subject: [PATCH 7/8] formatting --- .../vaadin/flow/component/contextmenu/it/SubMenuHelpersIT.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vaadin-context-menu-flow-parent/vaadin-context-menu-flow-integration-tests/src/test/java/com/vaadin/flow/component/contextmenu/it/SubMenuHelpersIT.java b/vaadin-context-menu-flow-parent/vaadin-context-menu-flow-integration-tests/src/test/java/com/vaadin/flow/component/contextmenu/it/SubMenuHelpersIT.java index 3a4f3cc6777..49ae8548ce5 100644 --- a/vaadin-context-menu-flow-parent/vaadin-context-menu-flow-integration-tests/src/test/java/com/vaadin/flow/component/contextmenu/it/SubMenuHelpersIT.java +++ b/vaadin-context-menu-flow-parent/vaadin-context-menu-flow-integration-tests/src/test/java/com/vaadin/flow/component/contextmenu/it/SubMenuHelpersIT.java @@ -26,8 +26,8 @@ import com.vaadin.flow.component.menubar.testbench.MenuBarElement; import com.vaadin.flow.component.notification.testbench.NotificationElement; -import com.vaadin.tests.AbstractComponentIT; import com.vaadin.flow.testutil.TestPath; +import com.vaadin.tests.AbstractComponentIT; @TestPath("vaadin-context-menu/sub-menu-helpers-test") public class SubMenuHelpersIT extends AbstractComponentIT { From 8242b60c5a5d0be2d8d44c969ea7cd4188174fdc Mon Sep 17 00:00:00 2001 From: Tatu Lund Date: Wed, 7 May 2025 09:37:20 +0300 Subject: [PATCH 8/8] formatting --- .../vaadin/flow/component/contextmenu/it/SubMenuHelpersIT.java | 1 - 1 file changed, 1 deletion(-) diff --git a/vaadin-context-menu-flow-parent/vaadin-context-menu-flow-integration-tests/src/test/java/com/vaadin/flow/component/contextmenu/it/SubMenuHelpersIT.java b/vaadin-context-menu-flow-parent/vaadin-context-menu-flow-integration-tests/src/test/java/com/vaadin/flow/component/contextmenu/it/SubMenuHelpersIT.java index 49ae8548ce5..16e703d9f63 100644 --- a/vaadin-context-menu-flow-parent/vaadin-context-menu-flow-integration-tests/src/test/java/com/vaadin/flow/component/contextmenu/it/SubMenuHelpersIT.java +++ b/vaadin-context-menu-flow-parent/vaadin-context-menu-flow-integration-tests/src/test/java/com/vaadin/flow/component/contextmenu/it/SubMenuHelpersIT.java @@ -25,7 +25,6 @@ import com.vaadin.flow.component.contextmenu.testbench.ContextMenuOverlayElement; import com.vaadin.flow.component.menubar.testbench.MenuBarElement; import com.vaadin.flow.component.notification.testbench.NotificationElement; - import com.vaadin.flow.testutil.TestPath; import com.vaadin.tests.AbstractComponentIT;