diff --git a/src/main/java/org/robotframework/swing/keyword/component/ComponentKeywords.java b/src/main/java/org/robotframework/swing/keyword/component/ComponentKeywords.java index 69595acf..b98a23bb 100644 --- a/src/main/java/org/robotframework/swing/keyword/component/ComponentKeywords.java +++ b/src/main/java/org/robotframework/swing/keyword/component/ComponentKeywords.java @@ -30,8 +30,8 @@ import org.robotframework.swing.factory.IdentifierParsingOperatorFactory; import org.robotframework.swing.util.ComponentExistenceResolver; import org.robotframework.swing.util.IComponentConditionResolver; +import org.robotframework.swing.util.ComponentUtils; -import javax.swing.*; import java.awt.*; import java.awt.event.InputEvent; import java.lang.reflect.Method; @@ -119,22 +119,12 @@ public void selectFromPopupMenu(String identifier, String menuPath) { @ArgumentNames({ "identifier", "menuPath" }) public List getMenuItemsFromPopupMenu(final String identifier, final String menuPath) { JPopupMenuOperator popup = operator(identifier).invokePopup(); - if(menuPath == null || "".equals(menuPath)) { - return getParsedElements(popup.getSubElements()); + if (menuPath == null || "".equals(menuPath)) { + return ComponentUtils.getParsedElements(popup.getSubElements()); } JMenuItemOperator subItem = popup.showMenuItem(menuPath); return subItem.getSubElements().length < 1 ? new ArrayList() : - getParsedElements(subItem.getSubElements()[0].getSubElements()); - } - - private List getParsedElements(MenuElement[] elements) { - List returnable = new ArrayList(); - for (MenuElement e : elements) { - if(JMenuItem.class.isAssignableFrom(e.getClass())) { - returnable.add(((JMenuItem)e).getText()); - } - } - return returnable; + ComponentUtils.getParsedElements(subItem.getSubElements()[0].getSubElements()); } @RobotKeyword("Checks that component is visible.\n" diff --git a/src/main/java/org/robotframework/swing/keyword/tree/TreeNodeKeywords.java b/src/main/java/org/robotframework/swing/keyword/tree/TreeNodeKeywords.java index cf807fe3..7fb7ecd4 100644 --- a/src/main/java/org/robotframework/swing/keyword/tree/TreeNodeKeywords.java +++ b/src/main/java/org/robotframework/swing/keyword/tree/TreeNodeKeywords.java @@ -24,6 +24,8 @@ import org.junit.Assert; +import org.netbeans.jemmy.operators.JMenuItemOperator; +import org.netbeans.jemmy.operators.JPopupMenuOperator; import org.robotframework.javalib.annotation.ArgumentNames; import org.robotframework.javalib.annotation.RobotKeyword; import org.robotframework.javalib.annotation.RobotKeywordOverload; @@ -31,6 +33,7 @@ import org.robotframework.swing.tree.TreeOperator; import org.robotframework.swing.tree.TreePathAction; import org.robotframework.swing.tree.TreeSupport; +import org.robotframework.swing.util.ComponentUtils; @RobotKeywords public class TreeNodeKeywords extends TreeSupport { @@ -105,6 +108,25 @@ public void selectTreeNode(String identifier, String nodeIdentifier, String[] ad } } + @RobotKeyword("Gets item names from the node context popup menu.\n" + + "Clears earlier selections.\n" + + "If several nodes have the same path then *only the first* menu item names of those nodes are returned.\n\n" + + "Example:\n" + + "| @{items}= | Get Node Items From Tree Popup Menu | _myTree_ | _Root|Folder_ | _Actions_ |\n" + + "| Should Contain | ${items} | _Do something_ |") + @ArgumentNames({"identifier", "nodeIdentifier", "menuPath"}) + public List getNodeItemsFromTreePopupMenu(String identifier, String nodeIdentifier, String menuPath) { + JPopupMenuOperator popupMenuOperator = treeOperator(identifier).createPopupOperator(nodeIdentifier); + + if (menuPath == null || menuPath.isEmpty()) { + return ComponentUtils.getParsedElements(popupMenuOperator.getSubElements()); + } else { + JMenuItemOperator subItem = popupMenuOperator.showMenuItem(menuPath); + return subItem.getSubElements().length < 1 ? new ArrayList() : + ComponentUtils.getParsedElements(subItem.getSubElements()[0].getSubElements()); + } + } + @RobotKeyword("Clicks on a tree node.\n\n" + "Examples:\n" + "| Click On Tree Node | _myTree_ | _Root|Folder_ |\n" diff --git a/src/main/java/org/robotframework/swing/util/ComponentUtils.java b/src/main/java/org/robotframework/swing/util/ComponentUtils.java new file mode 100644 index 00000000..fa29aafc --- /dev/null +++ b/src/main/java/org/robotframework/swing/util/ComponentUtils.java @@ -0,0 +1,35 @@ +/* + * Copyright 2008-2011 Nokia Siemens Networks Oyj + * + * 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 org.robotframework.swing.util; + +import javax.swing.*; +import java.util.ArrayList; +import java.util.List; + + +public class ComponentUtils { + public static List getParsedElements(MenuElement[] elements) { + List returnable = new ArrayList(); + for (MenuElement e : elements) { + if (JMenuItem.class.isAssignableFrom(e.getClass())) { + returnable.add(((JMenuItem) e).getText()); + } + } + return returnable; + } + +} diff --git a/src/test/resources/robot-tests/tree.robot b/src/test/resources/robot-tests/tree.robot index 69680c64..21c687bf 100644 --- a/src/test/resources/robot-tests/tree.robot +++ b/src/test/resources/robot-tests/tree.robot @@ -202,6 +202,12 @@ Get Tree Node Count Returns The Count Of All Visible Nodes ${visibleNodes}= getTreeNodeCount ${treeName} shouldBeEqualAsIntegers 2 ${visibleNodes} +Get Node Items From Tree Popup Menu + [Setup] resetNodes + ${popupMenuItems}= getNodeItemsFromTreePopupMenu ${treeName} ${leafNodePath} Submenu + ${expectedMenuItems}= createList Disabled menuitem Enabled menuitem + listsShouldBeEqual ${expectedMenuItems} ${popupMenuItems} + Get Tree Node Child Names [Setup] resetNodes ${expectedChildnames}= createList ${childNode1} ${childNode2}