Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added new keyword to retrieve items from Tree Node Popup Menu (#92) #93

Merged
merged 3 commits into from
Apr 25, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -119,22 +119,12 @@ public void selectFromPopupMenu(String identifier, String menuPath) {
@ArgumentNames({ "identifier", "menuPath" })
public List<String> 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<String>() :
getParsedElements(subItem.getSubElements()[0].getSubElements());
}

private List<String> getParsedElements(MenuElement[] elements) {
List<String> returnable = new ArrayList<String>();
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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,16 @@

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;
import org.robotframework.javalib.annotation.RobotKeywords;
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 {
Expand Down Expand Up @@ -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<String> 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<String>() :
ComponentUtils.getParsedElements(subItem.getSubElements()[0].getSubElements());
}
}

@RobotKeyword("Clicks on a tree node.\n\n"
+ "Examples:\n"
+ "| Click On Tree Node | _myTree_ | _Root|Folder_ |\n"
Expand Down
35 changes: 35 additions & 0 deletions src/main/java/org/robotframework/swing/util/ComponentUtils.java
Original file line number Diff line number Diff line change
@@ -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<String> getParsedElements(MenuElement[] elements) {
List<String> returnable = new ArrayList<String>();
for (MenuElement e : elements) {
if (JMenuItem.class.isAssignableFrom(e.getClass())) {
returnable.add(((JMenuItem) e).getText());
}
}
return returnable;
}

}
6 changes: 6 additions & 0 deletions src/test/resources/robot-tests/tree.robot
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down