From aa5f06f2cebf966e68d1326a20cf67cd9018c3ac Mon Sep 17 00:00:00 2001 From: John Doe Date: Wed, 7 Nov 2018 18:54:32 +0530 Subject: [PATCH] Fix for Add `Select From List Item Popup Menu` keyword #118 --- .../swing/keyword/list/ListKeywords.java | 22 ++++++++++++++++++- .../swing/list/ListOperator.java | 11 ++++++++++ .../swing/keyword/list/ListKeywordsSpec.java | 4 ++++ src/test/resources/robot-tests/list.robot | 6 +++++ 4 files changed, 42 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/robotframework/swing/keyword/list/ListKeywords.java b/src/main/java/org/robotframework/swing/keyword/list/ListKeywords.java index f700565d..63eee170 100644 --- a/src/main/java/org/robotframework/swing/keyword/list/ListKeywords.java +++ b/src/main/java/org/robotframework/swing/keyword/list/ListKeywords.java @@ -21,14 +21,18 @@ import org.apache.commons.collections.CollectionUtils; 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.common.IdentifierSupport; +import org.robotframework.swing.comparator.EqualsStringComparator; import org.robotframework.swing.factory.OperatorFactory; import org.robotframework.swing.list.ListOperator; import org.robotframework.swing.list.ListOperatorFactory; +import org.robotframework.swing.table.TableOperator; import org.robotframework.swing.util.SwingInvoker; @RobotKeywords @@ -143,7 +147,23 @@ public void listShouldNotContain(String identifier, String value) { Assert.assertTrue("List " + identifier + " contains " + value, !getListValues(identifier).contains(value)); } - + + @RobotKeyword("Selects an item from a list item popup.\n" + + "Separator for items is ``|``.\n\n" + + "Example:\n" + + "| `Select From List Item Popup Menu` | myList | listItem | File|Exit | ") + @ArgumentNames({"identifier", "listItemIdentifier", "menuPath"}) + public void selectFromListItemPopupMenu(String identifier, String listItemIdentifier, String menuPath) { + JMenuItemOperator menuItem = getPopupMenuItem(identifier, listItemIdentifier, menuPath); + menuItem.push(); + } + + private JMenuItemOperator getPopupMenuItem(String identifier, String listItemIdentifier, String menuPath) { + ListOperator listOperator = createOperator(identifier); + JPopupMenuOperator popupMenuOperator = listOperator.callPopupOnListItem(listItemIdentifier); + return popupMenuOperator.showMenuItem(menuPath, new EqualsStringComparator()); + } + private ListOperator createOperator(String identifier) { return operatorFactory.createOperator(identifier); } diff --git a/src/main/java/org/robotframework/swing/list/ListOperator.java b/src/main/java/org/robotframework/swing/list/ListOperator.java index 5890863b..abbf5f97 100644 --- a/src/main/java/org/robotframework/swing/list/ListOperator.java +++ b/src/main/java/org/robotframework/swing/list/ListOperator.java @@ -1,6 +1,8 @@ package org.robotframework.swing.list; import java.awt.Component; +import java.awt.Point; +import java.awt.event.InputEvent; import java.util.ArrayList; import java.util.List; @@ -10,6 +12,7 @@ import org.netbeans.jemmy.Waiter; import org.netbeans.jemmy.operators.ContainerOperator; import org.netbeans.jemmy.operators.JListOperator; +import org.netbeans.jemmy.operators.JPopupMenuOperator; import org.robotframework.swing.common.IdentifierSupport; import org.robotframework.swing.common.TimeoutCopier; import org.robotframework.swing.common.TimeoutName; @@ -57,6 +60,14 @@ public int getSize() { return jListOperator.getModel() .getSize(); } + + public JPopupMenuOperator callPopupOnListItem(String itemIdentifier) { + Point coordinates = jListOperator.indexToLocation(findIndex(itemIdentifier)); + return new JPopupMenuOperator(JPopupMenuOperator.callPopup(getSource(), + (int)coordinates.getX(), + (int)coordinates.getY(), + InputEvent.BUTTON3_MASK)); + } public void selectAll() { jListOperator.setSelectionInterval(0, getSize() - 1); diff --git a/src/test/java/org/robotframework/swing/keyword/list/ListKeywordsSpec.java b/src/test/java/org/robotframework/swing/keyword/list/ListKeywordsSpec.java index 6d38ef27..1e28e5d4 100644 --- a/src/test/java/org/robotframework/swing/keyword/list/ListKeywordsSpec.java +++ b/src/test/java/org/robotframework/swing/keyword/list/ListKeywordsSpec.java @@ -52,6 +52,10 @@ public void hasSelectAllListItemsKeyword() { public void hasClickOnListItemKeyword() { specify(context, satisfies(new RobotKeywordContract("clickOnListItem"))); } + + public void hasSelectFromListItemPopupMenuKeyword() { + specify(context, satisfies(new RobotKeywordContract("selectFromListItemPopupMenu"))); + } } public class Operating { diff --git a/src/test/resources/robot-tests/list.robot b/src/test/resources/robot-tests/list.robot index 27c6a79c..c464eafa 100644 --- a/src/test/resources/robot-tests/list.robot +++ b/src/test/resources/robot-tests/list.robot @@ -97,6 +97,12 @@ List Should Not Contain List Should Not Contain ${listName} missing element runKeywordAndExpectError List ${listName} contains one List Should Not Contain ${listName} one +Select From List Item Popup Menu + selectFromListItemPopupMenu ${listName} three Show name + dialogShouldBeOpen Message + selectDialog Message + pushButton OK + *** Keywords *** listSelectionShouldBe [Arguments] ${listIndex} ${expectedValue}