Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » SWTBot » Finding difficult to select an context menu
Finding difficult to select an context menu [message #36892] Tue, 26 May 2009 13:15 Go to next message
mahesh is currently offline maheshFriend
Messages: 1
Registered: July 2009
Junior Member
I have defined some menu declaratively in plugin.xml. And i want to select
menu at first level.
I am getting following error when i use below mentioned stt.

treeNode.select().contextMenu("<Name>").click();

stack trace:

org.eclipse.swtbot.swt.finder.exceptions.WidgetNotFoundExcep tion: The
widget {(an instance of org.eclipse.swt.widgets.MenuItem and with mnemonic
'<Menu Name>')} was disposed.
at
org.eclipse.swtbot.swt.finder.widgets.AbstractSWTBot.<init>(AbstractSWTBot.java:96)
at
org.eclipse.swtbot.swt.finder.widgets.SWTBotMenu.<init>(SWTBotMenu.java:42)
at
org.eclipse.swtbot.swt.finder.widgets.AbstractSWTBot.context Menu(AbstractSWTBot.java:383)
at
org.eclipse.swtbot.swt.finder.widgets.AbstractSWTBot.context Menu(AbstractSWTBot.java:356)
at
TestDeregisterMountArtifact.testDeregister(TestDeregisterMou ntArtifact.java:75)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAcce ssorImpl.java:39)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMe thodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at junit.framework.TestCase.runTest(TestCase.java:164)
at junit.framework.TestCase.runBare(TestCase.java:130)
at
org.eclipse.swtbot.swt.finder.SWTBotTestCase.runBare(SWTBotT estCase.java:228)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:120)
at
org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestRefer ence.run(JUnit3TestReference.java:130)
at
org.eclipse.jdt.internal.junit.runner.TestExecution.run(Test Execution.java:38)
at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTe sts(RemoteTestRunner.java:460)
at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTe sts(RemoteTestRunner.java:673)
at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(R emoteTestRunner.java:386)
at
org.eclipse.swtbot.eclipse.core.RemotePluginTestRunner.main( RemotePluginTestRunner.java:64)
at
org.eclipse.swtbot.eclipse.core.UITestApplication.runTests(U ITestApplication.java:123)
at
org.eclipse.ui.internal.testing.WorkbenchTestable$1.run(Work benchTestable.java:68)
at java.lang.Thread.run(Thread.java:619)
Re: Finding difficult to select an context menu [message #37532 is a reply to message #36892] Fri, 29 May 2009 10:49 Go to previous messageGo to next message
Sandeesh  is currently offline Sandeesh Friend
Messages: 1
Registered: July 2009
Junior Member
Hi,
Even I am facing the same problem.
In my case when the action is written programmatically, it works fine.

But when the same action is implemented in plugin.xml using the handlers
extension point I get the following error:
org.eclipse.swtbot.swt.finder.exceptions.WidgetNotFoundExcep tion: The
widget {(an instance of org.eclipse.swt.widgets.MenuItem and with mnemonic
'<Menu Name>')} was disposed.
Re: Finding difficult to select an context menu [message #38012 is a reply to message #37532] Sun, 31 May 2009 21:12 Go to previous messageGo to next message
Stefan Seelmann is currently offline Stefan SeelmannFriend
Messages: 11
Registered: July 2009
Junior Member
I have a slight different problem: My context menu is created
programmatically using IMenuListener.menuAboutToShow( IMenuManager )
method. I get the "disposed" exception when I try to access the menu at
the second level.

I created a small helper that works for me, see below. The idea is to
click the menu item _before_ sending the SWT.Hide event for the context
menu. Additionally the whole menu path is expected.

Maybe the others could test this snippet and report if it helps? If so I
would suggest to extend the existing API. The usage is
ContextMenuHelper.clickContextMenu(treeNode, "Import", "LDIF");

Kind Regards,
Stefan


------------------------------------------------------------ -------
import static
org.eclipse.swtbot.swt.finder.matchers.WidgetMatcherFactory. withMnemonic;
import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.instanceOf;

import java.util.Arrays;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swtbot.swt.finder.exceptions.WidgetNotFoundExcep tion;
import org.eclipse.swtbot.swt.finder.finders.UIThreadRunnable;
import org.eclipse.swtbot.swt.finder.results.VoidResult;
import org.eclipse.swtbot.swt.finder.results.WidgetResult;
import org.eclipse.swtbot.swt.finder.widgets.AbstractSWTBot;
import org.hamcrest.Matcher;

public class ContextMenuHelper {

/**
* Clicks the context menu matching the text.
*
* @param text
* the text on the context menu.
* @throws WidgetNotFoundException
* if the widget is not found.
*/
public static void clickContextMenu(final AbstractSWTBot<?> bot,
final String... texts) {

// show
final MenuItem menuItem = UIThreadRunnable
.syncExec(new WidgetResult<MenuItem>() {
public MenuItem run() {
MenuItem menuItem = null;
Control control = (Control) bot.widget;
Menu menu = control.getMenu();
for (String text : texts) {
Matcher<?> matcher = allOf(instanceOf(MenuItem.class),
withMnemonic(text));
menuItem = show(menu, matcher);
if (menuItem != null) {
menu = menuItem.getMenu();
} else {
hide(menu);
break;
}
}

return menuItem;
}
});
if (menuItem == null) {
throw new WidgetNotFoundException("Could not find menu: "
+ Arrays.asList(texts));
}

// click
click(menuItem);

// hide
UIThreadRunnable.syncExec(new VoidResult() {
public void run() {
hide(menuItem.getParent());
}
});
}

private static MenuItem show(final Menu menu, final Matcher<?> matcher) {
if (menu != null) {
menu.notifyListeners(SWT.Show, new Event());
MenuItem[] items = menu.getItems();
for (final MenuItem menuItem : items) {
if (matcher.matches(menuItem)) {
return menuItem;
}
}
menu.notifyListeners(SWT.Hide, new Event());
}
return null;
}

private static void click(final MenuItem menuItem) {
final Event event = new Event();
event.time = (int) System.currentTimeMillis();
event.widget = menuItem;
event.display = menuItem.getDisplay();
event.type = SWT.Selection;

UIThreadRunnable.asyncExec(menuItem.getDisplay(), new VoidResult() {
public void run() {
menuItem.notifyListeners(SWT.Selection, event);
}
});
}

private static void hide(final Menu menu) {
menu.notifyListeners(SWT.Hide, new Event());
if (menu.getParentMenu() != null) {
hide(menu.getParentMenu());
}
}
}
Re: Finding difficult to select an context menu [message #479900 is a reply to message #38012] Thu, 13 August 2009 00:11 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: jconlon.apache.org

Hi Stefan,

I found your helper in the newsgroup. It's worked well for me.

Added a few log statements to help troubleshoot those pesky 'spaces in
the wrong places' bugs that can really be a pain to find.

See below...

thanks for the suggestion,
John

Stefan Seelmann wrote:
> I have a slight different problem: My context menu is created
> programmatically using IMenuListener.menuAboutToShow( IMenuManager )
> method. I get the "disposed" exception when I try to access the menu at
> the second level.
>
> I created a small helper that works for me, see below. The idea is to
> click the menu item _before_ sending the SWT.Hide event for the context
> menu. Additionally the whole menu path is expected.
>
> Maybe the others could test this snippet and report if it helps? If so I
> would suggest to extend the existing API. The usage is
> ContextMenuHelper.clickContextMenu(treeNode, "Import", "LDIF");
>
> Kind Regards,
> Stefan
>
>
> ------------------------------------------------------------ -------
> import static
> org.eclipse.swtbot.swt.finder.matchers.WidgetMatcherFactory. withMnemonic;
> import static org.hamcrest.Matchers.allOf;
> import static org.hamcrest.Matchers.instanceOf;
>
> import java.util.Arrays;
>
> import org.eclipse.swt.SWT;
> import org.eclipse.swt.widgets.Control;
> import org.eclipse.swt.widgets.Event;
> import org.eclipse.swt.widgets.Menu;
> import org.eclipse.swt.widgets.MenuItem;
> import org.eclipse.swtbot.swt.finder.exceptions.WidgetNotFoundExcep tion;
> import org.eclipse.swtbot.swt.finder.finders.UIThreadRunnable;
> import org.eclipse.swtbot.swt.finder.results.VoidResult;
> import org.eclipse.swtbot.swt.finder.results.WidgetResult;
> import org.eclipse.swtbot.swt.finder.widgets.AbstractSWTBot;
> import org.hamcrest.Matcher;
>
> public class ContextMenuHelper {
>
private static Logger log = Logger.getLogger(ContextMenuHelper.class);
> /**
> * Clicks the context menu matching the text.
> *
> * @param text
> * the text on the context menu.
> * @throws WidgetNotFoundException
> * if the widget is not found.
> */
> public static void clickContextMenu(final AbstractSWTBot<?> bot,
> final String... texts) {
>
log.debug("Searching for "+Arrays.asList(texts) );
> // show
> final MenuItem menuItem = UIThreadRunnable
> .syncExec(new WidgetResult<MenuItem>() {
> public MenuItem run() {
> MenuItem menuItem = null;
> Control control = (Control) bot.widget;
> Menu menu = control.getMenu();
> for (String text : texts) {
log.debug("Matching:<"+text+'>');
> Matcher<?> matcher = allOf(instanceOf(MenuItem.class),
> withMnemonic(text));
> menuItem = show(menu, matcher);
> if (menuItem != null) {
> menu = menuItem.getMenu();
> } else {
> hide(menu);
> break;
> }
> }
>
> return menuItem;
> }
> });
> if (menuItem == null) {
> throw new WidgetNotFoundException("Could not find menu: "
> + Arrays.asList(texts));
> }
>
> // click
> click(menuItem);
>
> // hide
> UIThreadRunnable.syncExec(new VoidResult() {
> public void run() {
> hide(menuItem.getParent());
> }
> });
> }
>
> private static MenuItem show(final Menu menu, final Matcher<?> matcher) {
> if (menu != null) {
> menu.notifyListeners(SWT.Show, new Event());
> MenuItem[] items = menu.getItems();
> for (final MenuItem menuItem : items) {
> if (matcher.matches(menuItem)) {
log.debug("Found menuItem match: <"+menuItem.getText()+'>');
> return menuItem;
> }
else{
log.debug("Rejecting menuItem match:<"+
menuItem.getText()+'>');
}
> }
> menu.notifyListeners(SWT.Hide, new Event());
> }
> return null;
> }
>
> private static void click(final MenuItem menuItem) {
> final Event event = new Event();
> event.time = (int) System.currentTimeMillis();
> event.widget = menuItem;
> event.display = menuItem.getDisplay();
> event.type = SWT.Selection;
>
> UIThreadRunnable.asyncExec(menuItem.getDisplay(), new VoidResult() {
> public void run() {
> menuItem.notifyListeners(SWT.Selection, event);
> }
> });
> }
>
> private static void hide(final Menu menu) {
> menu.notifyListeners(SWT.Hide, new Event());
> if (menu.getParentMenu() != null) {
> hide(menu.getParentMenu());
> }
> }
> }
Re: Finding difficult to select an context menu [message #488405 is a reply to message #36892] Mon, 28 September 2009 14:42 Go to previous messageGo to next message
Michelle Davidson is currently offline Michelle DavidsonFriend
Messages: 41
Registered: August 2009
Member
Hello

I want to use the ContextMenuhelper for click a contextmenu for a treeItem.

My test looks follow.

        SWTBotTree tree = bot.treeWithId("folderTree");
        SWTBotTreeItem treeItem = tree.getTreeItem("TestFolder 01").select();
        ContextMenuHelper.clickContextMenu(treeItem, "Properties");


But when I run the test I get the folow Exception.

org.eclipse.swt.SWTException: Failed to execute runnable (java.lang.ClassCastException: org.eclipse.swt.widgets.TreeItem cannot be cast to org.eclipse.swt.widgets.Control

What must I change that I can click the contextMenu of a treeitem?
Re: Finding difficult to select an context menu [message #488452 is a reply to message #488405] Mon, 28 September 2009 17:23 Go to previous messageGo to next message
Pascal G is currently offline Pascal GFriend
Messages: 157
Registered: July 2009
Senior Member
Michelle Davidson wrote:
> Hello
> I want to use the ContextMenuhelper for click a contextmenu for a treeItem.
>
> My test looks follow.
>
> SWTBotTree tree = bot.treeWithId("folderTree");
> SWTBotTreeItem treeItem = tree.getTreeItem("TestFolder
> 01").select();
> ContextMenuHelper.clickContextMenu(treeItem, "Properties");
>
> But when I run the test I get the folow Exception.
>
> org.eclipse.swt.SWTException: Failed to execute runnable
> (java.lang.ClassCastException: org.eclipse.swt.widgets.TreeItem cannot
> be cast to org.eclipse.swt.widgets.Control
>
> What must I change that I can click the contextMenu of a treeitem?

The ContextMenuHelper works only for Control subclasses. TreeItem is not
a subclass of Control and thus cannot work. It would suggest passing the
tree instead and see what happens (keep the rest of the code as is):
SWTBotTree tree = bot.treeWithId("folderTree");
SWTBotTreeItem treeItem = tree.getTreeItem("TestFolder
01").select();
ContextMenuHelper.clickContextMenu(tree, "Properties");

On a side-note, the signature of that method should be changed to:
public static void clickContextMenu(final AbstractSWTBot<? extends
Control, final String...).
It would also render the cast in "Control control = (Control)
bot.widget;" obsolete.

Hope this help.
--
Pascal GĂ©linas | Software Developer
*Nu Echo Inc.*
http://www.nuecho.com/ | http://blog.nuecho.com/

*Because performance matters.*
Re: Finding difficult to select an context menu [message #489602 is a reply to message #488452] Mon, 05 October 2009 09:30 Go to previous messageGo to next message
as Missing name is currently offline as Missing nameFriend
Messages: 33
Registered: July 2009
Member
Pascal Gelinas schrieb:
> Michelle Davidson wrote:
>> Hello
>> I want to use the ContextMenuhelper for click a contextmenu for a
>> treeItem.
>>
>> My test looks follow.
>>
>> SWTBotTree tree = bot.treeWithId("folderTree");
>> SWTBotTreeItem treeItem = tree.getTreeItem("TestFolder
>> 01").select();
>> ContextMenuHelper.clickContextMenu(treeItem, "Properties");
>>
>> But when I run the test I get the folow Exception.
>>
>> org.eclipse.swt.SWTException: Failed to execute runnable
>> (java.lang.ClassCastException: org.eclipse.swt.widgets.TreeItem cannot
>> be cast to org.eclipse.swt.widgets.Control
>>
>> What must I change that I can click the contextMenu of a treeitem?
>
> The ContextMenuHelper works only for Control subclasses. TreeItem is not
> a subclass of Control and thus cannot work. It would suggest passing the
> tree instead and see what happens (keep the rest of the code as is):
> SWTBotTree tree = bot.treeWithId("folderTree");
> SWTBotTreeItem treeItem = tree.getTreeItem("TestFolder
> 01").select();
> ContextMenuHelper.clickContextMenu(tree, "Properties");
>
> On a side-note, the signature of that method should be changed to:
> public static void clickContextMenu(final AbstractSWTBot<? extends
> Control, final String...).
> It would also render the cast in "Control control = (Control)
> bot.widget;" obsolete.
>
> Hope this help.


Seems to work only for the first level entries.
How can i click for example on the entry "New" -> "Folder" ?
Re: Finding difficult to select an context menu [message #489757 is a reply to message #489602] Mon, 05 October 2009 19:15 Go to previous messageGo to next message
Pascal G is currently offline Pascal GFriend
Messages: 157
Registered: July 2009
Senior Member
Aleksey Shumilin wrote:
> Seems to work only for the first level entries.
> How can i click for example on the entry "New" -> "Folder" ?

From my understanding of the code, this should work (I have not tested
this myself by it seems correct). To click on "New" -> "Folder", do the
following:
ContextMenuHelper.clickContextMenu(bot, "New, "Folder");
Re: Finding difficult to select an context menu [message #489843 is a reply to message #489757] Tue, 06 October 2009 08:30 Go to previous messageGo to next message
as Missing name is currently offline as Missing nameFriend
Messages: 33
Registered: July 2009
Member
Pascal Gelinas schrieb:
> Aleksey Shumilin wrote:
>> Seems to work only for the first level entries.
>> How can i click for example on the entry "New" -> "Folder" ?
>
> From my understanding of the code, this should work (I have not tested
> this myself by it seems correct). To click on "New" -> "Folder", do the
> following:
> ContextMenuHelper.clickContextMenu(bot, "New, "Folder");


oh yes, it works, thank you Pascal !
Re: Finding difficult to select an context menu [message #491152 is a reply to message #38012] Tue, 13 October 2009 13:30 Go to previous messageGo to next message
Michelle Davidson is currently offline Michelle DavidsonFriend
Messages: 41
Registered: August 2009
Member
Stefan Seelmann wrote on Sun, 31 May 2009 17:12
I have a slight different problem: My context menu is created
programmatically using IMenuListener.menuAboutToShow( IMenuManager )
method. I get the "disposed" exception when I try to access the menu at
the second level.

I created a small helper that works for me, see below. The idea is to
click the menu item _before_ sending the SWT.Hide event for the context
menu. Additionally the whole menu path is expected.

Maybe the others could test this snippet and report if it helps? If so I
would suggest to extend the existing API. The usage is
ContextMenuHelper.clickContextMenu(treeNode, "Import", "LDIF");

Kind Regards,


Stefan


------------------------------------------------------------ -------
import static
org.eclipse.swtbot.swt.finder.matchers.WidgetMatcherFactory. withMnemonic;
import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.instanceOf;

import java.util.Arrays;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swtbot.swt.finder.exceptions.WidgetNotFoundExcep tion;
import org.eclipse.swtbot.swt.finder.finders.UIThreadRunnable;
import org.eclipse.swtbot.swt.finder.results.VoidResult;
import org.eclipse.swtbot.swt.finder.results.WidgetResult;
import org.eclipse.swtbot.swt.finder.widgets.AbstractSWTBot;
import org.hamcrest.Matcher;

public class ContextMenuHelper {

/**
* Clicks the context menu matching the text.
*
* @param text
* the text on the context menu.
* @throws WidgetNotFoundException
* if the widget is not found.
*/
public static void clickContextMenu(final AbstractSWTBot<?> bot,
final String... texts) {

// show
final MenuItem menuItem = UIThreadRunnable
.syncExec(new WidgetResult<MenuItem>() {
public MenuItem run() {
MenuItem menuItem = null;
Control control = (Control) bot.widget;
Menu menu = control.getMenu();
for (String text : texts) {
Matcher<?> matcher = allOf(instanceOf(MenuItem.class),
withMnemonic(text));
menuItem = show(menu, matcher);
if (menuItem != null) {
menu = menuItem.getMenu();
} else {
hide(menu);
break;
}
}

return menuItem;
}
});
if (menuItem == null) {
throw new WidgetNotFoundException("Could not find menu: "
+ Arrays.asList(texts));
}

// click
click(menuItem);

// hide
UIThreadRunnable.syncExec(new VoidResult() {
public void run() {
hide(menuItem.getParent());
}
});
}

private static MenuItem show(final Menu menu, final Matcher<?> matcher) {
if (menu != null) {
menu.notifyListeners(SWT.Show, new Event());
MenuItem[] items = menu.getItems();
for (final MenuItem menuItem : items) {
if (matcher.matches(menuItem)) {
return menuItem;
}
}
menu.notifyListeners(SWT.Hide, new Event());
}
return null;
}

private static void click(final MenuItem menuItem) {
final Event event = new Event();
event.time = (int) System.currentTimeMillis();
event.widget = menuItem;
event.display = menuItem.getDisplay();
event.type = SWT.Selection;

UIThreadRunnable.asyncExec(menuItem.getDisplay(), new VoidResult() {
public void run() {
menuItem.notifyListeners(SWT.Selection, event);
}
});
}

private static void hide(final Menu menu) {
menu.notifyListeners(SWT.Hide, new Event());
if (menu.getParentMenu() != null) {
hide(menu.getParentMenu());
}
}
}


Hello Stefan.

Have you already suggest to extend the API, because I think your ContextmenHelper works fine. And so could everybody use it, without looking for this thread. Smile

Re: Finding difficult to select an context menu [message #523318 is a reply to message #491152] Thu, 25 March 2010 21:19 Go to previous messageGo to next message
Steven Chamberlin is currently offline Steven ChamberlinFriend
Messages: 30
Registered: July 2009
Member
Hi, is this functionality included in the latest version of SWT-Bot?

Also, my problem is not with a tree but rather with a context menu invoked from an editor, i.e.:

bot.activeEditor().toTextEditor().contextMenu("menu1").contextMenu( "menu2").click();

Could the ContextMenuHelper class be extended for editors too?

Thanks!

-- Steve
Re: Finding difficult to select an context menu [message #629948 is a reply to message #523318] Thu, 30 September 2010 06:07 Go to previous messageGo to next message
Jinzi  is currently offline Jinzi Friend
Messages: 1
Registered: September 2010
Junior Member
Hi Steve,

Try
bot.activeEditor().toTextEditor().contextMenu( "menu2").click();
without the first contextMenu.

Jin
Re: Finding difficult to select an context menu [message #664833 is a reply to message #36892] Tue, 12 April 2011 08:56 Go to previous messageGo to next message
Stefan Schaefer is currently offline Stefan SchaeferFriend
Messages: 2
Registered: April 2011
Junior Member
Hi all,
hopefully no one is offended by me pulling up this old thread.
But I find the proposed ContextMenuHelper of Stefan Seelmann very useful. Thus I'd once again vote for taking this into SWTBot by default Smile

There's only one small enhancement I needed to add to make this work for me: The MenuDetect-Event. So, before calling the menu of the control, I fire the mentioned event, so that the listeners may prepare for (or prevent) the opening of the menu.

Below is the complete code with my changes

import static org.eclipse.swtbot.swt.finder.matchers.WidgetMatcherFactory.withMnemonic;
import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.instanceOf;

import java.util.Arrays;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swtbot.swt.finder.exceptions.WidgetNotFoundException;
import org.eclipse.swtbot.swt.finder.finders.UIThreadRunnable;
import org.eclipse.swtbot.swt.finder.results.VoidResult;
import org.eclipse.swtbot.swt.finder.results.WidgetResult;
import org.eclipse.swtbot.swt.finder.widgets.AbstractSWTBot;
import org.hamcrest.Matcher;

/**
 * This helper is a workaround for a bug in SWTBot,
 * where the bot can't find a dynamically created context menu
 * @author Stefan Seelmann (initial)
 * @author Stefan Schaefer (extension)
 */
public class ContextMenuHelper {

	/**
	 * Clicks the context menu matching the text.
	 * 
	 * @param text
	 *            the text on the context menu.
	 * @throws WidgetNotFoundException
	 *             if the widget is not found.
	 */
	public static void clickContextMenu(final AbstractSWTBot<?> bot,
			final String... texts) {

		// show
		final MenuItem menuItem = UIThreadRunnable
				.syncExec(new WidgetResult<MenuItem>() {
					public MenuItem run() {
						MenuItem menuItem = null;
						Control control = (Control) bot.widget;
						
						//MenuDetectEvent added by Stefan Schaefer
						Event event = new Event();
						control.notifyListeners(SWT.MenuDetect, event);
						if (!event.doit) {
							return null;
						}
						
						Menu menu = control.getMenu();
						for (String text : texts) {
							@SuppressWarnings("unchecked")
							Matcher<?> matcher = allOf(
									instanceOf(MenuItem.class),
									withMnemonic(text));
							menuItem = show(menu, matcher);
							if (menuItem != null) {
								menu = menuItem.getMenu();
							} else {
								hide(menu);
								break;
							}
						}

						return menuItem;
					}
				});
		if (menuItem == null) {
			throw new WidgetNotFoundException("Could not find menu: "
					+ Arrays.asList(texts));
		}

		// click
		click(menuItem);

		// hide
		UIThreadRunnable.syncExec(new VoidResult() {
			public void run() {
				hide(menuItem.getParent());
			}
		});
	}

	private static MenuItem show(final Menu menu, final Matcher<?> matcher) {
		if (menu != null) {
			menu.notifyListeners(SWT.Show, new Event());
			MenuItem[] items = menu.getItems();
			for (final MenuItem menuItem : items) {
				if (matcher.matches(menuItem)) {
					return menuItem;
				}
			}
			menu.notifyListeners(SWT.Hide, new Event());
		}
		return null;
	}

	private static void click(final MenuItem menuItem) {
		final Event event = new Event();
		event.time = (int) System.currentTimeMillis();
		event.widget = menuItem;
		event.display = menuItem.getDisplay();
		event.type = SWT.Selection;

		UIThreadRunnable.asyncExec(menuItem.getDisplay(), new VoidResult() {
			public void run() {
				menuItem.notifyListeners(SWT.Selection, event);
			}
		});
	}

	private static void hide(final Menu menu) {
		menu.notifyListeners(SWT.Hide, new Event());
		if (menu.getParentMenu() != null) {
			hide(menu.getParentMenu());
		}
	}
}

icon14.gif  Re: Finding difficult to select an context menu [message #667240 is a reply to message #664833] Wed, 27 April 2011 15:11 Go to previous messageGo to next message
Nicolas Guyomar is currently offline Nicolas GuyomarFriend
Messages: 29
Registered: February 2010
Location: Nantes (France)
Junior Member
Hi Stefan,

I'm glad you pulled up this thread.
+1 for taking this into SWTBot by default since Bug 261360 is still pending.

Thank you

Nicolas Guyomar
http://www.eclipse.org/MoDisco/
http://www.eclipse.org/modeling/emft/facet/


Re: Finding difficult to select an context menu [message #924991 is a reply to message #36892] Thu, 27 September 2012 09:44 Go to previous messageGo to next message
Syamananda Singh is currently offline Syamananda SinghFriend
Messages: 4
Registered: September 2012
Junior Member
Hi All,

I'm having difficulty in getting the context menu of a tree/tree item of a certain view.
I used the clickContextMenu method of ContextMenuHelper, but strangely it's not working as I expected.

If i select a tree item and then call the clickContextMenu by passing the tree, it throws org.eclipse.core.runtime.AssertionFailedException: null argument:Action must not be null.

The logic of my test :

- open the view : Breakpoints view
- Get the tree and its subsequent tree item
- Select the tree item which has the given text
- open the context menu (Breakpoint Properties...) and select it to open the properties dialog

The code looks something like this :

public void openBreakPointProperties(String treeItemName) {

SWTBotView breakpointView = bot.viewByTitle("Breakpoints");
breakpointView.show();
breakpointView.setFocus();

SWTBotTree breakpointTree = bot.tree();
//ContextMenuHelper.clickContextMenu(breakpointTree, "Breakpoint Properties...");
// Works just fine...opens the context menu


for (SWTBotTreeItem treeItem : breakpointTree.getAllItems()) {
if (treeItem.select().getText().equalsIgnoreCase(treeItemName)) {
// condition reaches here, but the below clickContextMenu fails..even if i pass bot.tree() directly

ContextMenuHelper.clickContextMenu(breakpointTree, "Breakpoint Properties..."); // Getting org.eclipse.core.runtime.AssertionFailedException: null argument:Action must not be null
break;
}
}
}

Can please assist me whats wrong here in clickContextMenu method of ContextMenuHelper ?

The error log is :

org.eclipse.swt.SWTException: Failed to execute runnable (org.eclipse.core.runtime.AssertionFailedException: null argument:Action must not be null)
at org.eclipse.swt.SWT.error(SWT.java:4282)
at org.eclipse.swt.SWT.error(SWT.java:4197)
at org.eclipse.swt.widgets.Synchronizer.syncExec(Synchronizer.java:196)
at org.eclipse.ui.internal.UISynchronizer.syncExec(UISynchronizer.java:150)
at org.eclipse.swt.widgets.Display.syncExec(Display.java:4683)
at org.eclipse.swtbot.swt.finder.finders.UIThreadRunnable.run(UIThreadRunnable.java:76)
at org.eclipse.swtbot.swt.finder.finders.UIThreadRunnable.syncExec(UIThreadRunnable.java:142)
at org.eclipse.swtbot.swt.finder.finders.UIThreadRunnable.syncExec(UIThreadRunnable.java:125)
at com.hp.nsdee.tests.qa.debugger.ContextMenuHelper.clickContextMenu(ContextMenuHelper.java:45)
at com.hp.nsdee.tests.qa.debugger.DebuggerTestHelper.setCommonBreakPointProperties(DebuggerTestHelper.java:457)
at com.hp.nsdee.tests.qa.debugger.SampleTest.test(SampleTest.java:99)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.eclipse.swtbot.swt.finder.junit.SWTBotJunit4ClassRunner.run(SWTBotJunit4ClassRunner.java:54)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.swtbot.eclipse.core.RemotePluginTestRunner.main(RemotePluginTestRunner.java:64)
at org.eclipse.swtbot.eclipse.core.UITestApplication.runTests(UITestApplication.java:117)
at org.eclipse.ui.internal.testing.WorkbenchTestable$1.run(WorkbenchTestable.java:71)
at java.lang.Thread.run(Unknown Source)
Caused by: org.eclipse.core.runtime.AssertionFailedException: null argument:Action must not be null
at org.eclipse.core.runtime.Assert.isNotNull(Assert.java:85)
at org.eclipse.jface.action.ContributionManager.add(ContributionManager.java:75)
at org.eclipse.debug.internal.ui.views.breakpoints.BreakpointsView.fillContextMenu(BreakpointsView.java:220)
at org.eclipse.debug.ui.AbstractDebugView$2.menuAboutToShow(AbstractDebugView.java:535)
at org.eclipse.jface.action.MenuManager.fireAboutToShow(MenuManager.java:342)
at org.eclipse.jface.action.MenuManager.handleAboutToShow(MenuManager.java:473)
at org.eclipse.jface.action.MenuManager.access$1(MenuManager.java:469)
at org.eclipse.jface.action.MenuManager$2.menuShown(MenuManager.java:495)
at org.eclipse.swt.widgets.TypedListener.handleEvent(TypedListener.java:247)
at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1053)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1077)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1062)
at org.eclipse.swt.widgets.Widget.notifyListeners(Widget.java:774)
at com.mytest.tests.qa.debugger.ContextMenuHelper.show(ContextMenuHelper.java:98)
Re: Finding difficult to select an context menu [message #925026 is a reply to message #924991] Thu, 27 September 2012 10:12 Go to previous messageGo to next message
Mickael Istria is currently offline Mickael IstriaFriend
Messages: 769
Registered: July 2009
Location: Grenoble, France
Senior Member
See https://bugs.eclipse.org/bugs/show_bug.cgi?id=384621
Please give a try to suggested solution in bug and share feedback.

Cheers,
--
Mickael Istria
JBoss, by Red Hat
My blog: http://mickaelistria.wordpress.com
My Tweets: http://twitter.com/mickaelistria
Re: Finding difficult to select an context menu [message #994567 is a reply to message #925026] Thu, 27 December 2012 17:47 Go to previous messageGo to next message
Shabari shetty is currently offline Shabari shettyFriend
Messages: 5
Registered: December 2012
Junior Member
Hi,

I am trying to click the submenu of a context menu on a tree item using ContextMenuHelper. My code looks like below

SWTBotView view = bot.viewByTitle("Package Explorer");
view.setFocus();
SWTBotTree tree = view.bot().tree();
// tree.getAllItems()
SWTBotTreeItem treeItem = view.bot().tree()
.getTreeItem("JavaPrj");
treeItem.expand();
bot.sleep(1000);
SWTBotTreeItem[] items = treeItem.getItems();
SWTBotTreeItem srcNode2 = null;
SWTBotTreeItem javaClass = null;
for(SWTBotTreeItem prjNode : items){
if(prjNode.getText().equals("src")){
prjNode.expand();
SWTBotTreeItem [] srcNodes = prjNode.getItems();

for(SWTBotTreeItem srcNode : srcNodes){
srcNode.expand();
javaClass = srcNode.getItems()[0];

}
}
}
javaClass.select();
ContextMenuHelper.clickContextMenu(tree, "Run As","Run on Server");

Here I am trying to select a tree item node which is a java class then say Run As->Run On Server. I get the excatpion widget not found (Run As, Run on Server) exception. Please note that my context menu is not populated programmatic-ally. I am also attaching the class ContextMenuHelper.java which I am using. Can someone please tell m,e what do I need to change to make this working

Thanks in advance
Shabari
Re: Finding difficult to select an context menu [message #994818 is a reply to message #994567] Fri, 28 December 2012 11:00 Go to previous messageGo to next message
Lorenzo Bettini is currently offline Lorenzo BettiniFriend
Messages: 1397
Registered: July 2009
Senior Member
Hi

if you see https://bugs.eclipse.org/bugs/show_bug.cgi?id=384621 and use
the update site you find there, there should be no need to use the
ContextMenuHelper manually, since the SWTBotTreeItem.contextMenu should
be fixed (it uses internally the ContextMenuHelper)...

are you sure that "Run As","Run on Server" are exactly the labels of the
context menu? No "..." anywhere?

hope this helps
Lorenzo

On 12/27/2012 06:47 PM, Shabari shetty wrote:
> Hi,
>
> I am trying to click the submenu of a context menu on a tree item using ContextMenuHelper. My code looks like below
>
> SWTBotView view = bot.viewByTitle("Package Explorer");
> view.setFocus();
> SWTBotTree tree = view.bot().tree();
> // tree.getAllItems()
> SWTBotTreeItem treeItem = view.bot().tree()
> .getTreeItem("JavaPrj");
> treeItem.expand();
> bot.sleep(1000);
> SWTBotTreeItem[] items = treeItem.getItems();
> SWTBotTreeItem srcNode2 = null;
> SWTBotTreeItem javaClass = null;
> for(SWTBotTreeItem prjNode : items){
> if(prjNode.getText().equals("src")){
> prjNode.expand();
> SWTBotTreeItem [] srcNodes = prjNode.getItems();
>
> for(SWTBotTreeItem srcNode : srcNodes){
> srcNode.expand();
> javaClass = srcNode.getItems()[0];
>
> }
> }
> }
> javaClass.select();
> ContextMenuHelper.clickContextMenu(tree, "Run As","Run on Server");
>
> Here I am trying to select a tree item node which is a java class then say Run As->Run On Server. I get the excatpion widget not found (Run As, Run on Server) exception. Please note that my context menu is not populated programmatic-ally. I am also attaching the class ContextMenuHelper.java which I am using. Can someone please tell m,e what do I need to change to make this working
>
> Thanks in advance
> Shabari
>


--
Lorenzo Bettini, PhD in Computer Science, DI, Univ. Torino
ICQ# lbetto, 16080134 (GNU/Linux User # 158233)
HOME: http://www.lorenzobettini.it MUSIC: http://www.purplesucker.com
http://www.myspace.com/supertrouperabba
BLOGS: http://tronprog.blogspot.com http://longlivemusic.blogspot.com
http://www.gnu.org/software/src-highlite
http://www.gnu.org/software/gengetopt
http://www.gnu.org/software/gengen http://doublecpp.sourceforge.net
Re: Finding difficult to select an context menu [message #1008712 is a reply to message #994818] Wed, 13 February 2013 16:41 Go to previous messageGo to next message
Joey Mink is currently offline Joey MinkFriend
Messages: 87
Registered: July 2009
Location: Centreville, VA, USA
Member

To ease the work for others, the update site referenced by the bug mentioned above is:

http://download.eclipse.org/technology/swtbot/snapshots/

[Updated on: Wed, 13 February 2013 16:44]

Report message to a moderator

Re: Finding difficult to select an context menu [message #1008720 is a reply to message #1008712] Wed, 13 February 2013 17:40 Go to previous messageGo to next message
Joey Mink is currently offline Joey MinkFriend
Messages: 87
Registered: July 2009
Location: Centreville, VA, USA
Member

Seems like this may still be an issue for accessing editor context menus. I installed 2.1.0.201302091231, and got the following stack trace:

org.eclipse.swtbot.swt.finder.exceptions.WidgetNotFoundException: The widget {(of type 'MenuItem' and with mnemonic 'Resolve icon')} was disposed.
	at org.eclipse.swtbot.swt.finder.widgets.AbstractSWTBot.<init>(AbstractSWTBot.java:106)
	at org.eclipse.swtbot.swt.finder.widgets.SWTBotMenu.<init>(SWTBotMenu.java:42)
	at org.eclipse.swtbot.swt.finder.widgets.AbstractSWTBot.contextMenu(AbstractSWTBot.java:460)
	at org.eclipse.swtbot.swt.finder.widgets.AbstractSWTBot.contextMenu(AbstractSWTBot.java:430)
	at org.eclipse.swtbot.eclipse.finder.widgets.SWTBotEclipseEditor.contextMenu(SWTBotEclipseEditor.java:269)
	at com.exoanalytic.seas.ide.editor.ResolveIconHandlerTest.testResolveIcon(ResolveIconHandlerTest.java:22)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
	at java.lang.reflect.Method.invoke(Method.java:597)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
	at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
	at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
	at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
	at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
	at org.eclipse.swtbot.eclipse.core.RemotePluginTestRunner.main(RemotePluginTestRunner.java:64)
	at org.eclipse.swtbot.eclipse.core.UITestApplication.runTests(UITestApplication.java:117)
	at org.eclipse.ui.internal.testing.WorkbenchTestable$1.run(WorkbenchTestable.java:71)
	at java.lang.Thread.run(Thread.java:680)


From the following code:

		SWTBotEclipseEditor editor = bot.activeEditor().toTextEditor();
		editor.navigateTo(9, 3);
		editor.contextMenu("Resolve icon").click();
		SWTBotShell dialog = bot.activeShell();
Re: Finding difficult to select an context menu [message #1021137 is a reply to message #1008720] Tue, 19 March 2013 15:12 Go to previous messageGo to next message
Craig Foote is currently offline Craig FooteFriend
Messages: 195
Registered: July 2009
Senior Member
The ContextMenuHelper works for me running locally but not under Tycho. In #show(Menu, Matcher) this returns an empty array:

menu.notifyListeners(SWT.SHOW, new Event())
MenuItems[] items = menu.getItems()


Any ideas?
Craig
Re: Finding difficult to select an context menu [message #1021727 is a reply to message #1021137] Wed, 20 March 2013 15:26 Go to previous messageGo to next message
Mickael Istria is currently offline Mickael IstriaFriend
Messages: 769
Registered: July 2009
Location: Grenoble, France
Senior Member
On 03/19/2013 04:12 PM, Craig Foote wrote:
> The ContextMenuHelper works for me running locally but not under Tycho.
> In #show(Menu, Matcher) this returns an empty array:
>
>
> menu.notifyListeners(SWT.SHOW, new Event())
> MenuItems[] items = menu.getItems()

Have you checked <useUIThread> is set to false in your
tycho-surefile-plugin configuration? Also, if you're on Linux, do you
have a Window Mananger started in your test DISPLAY?

--
Mickael Istria
JBoss, by Red Hat
My blog: http://mickaelistria.wordpress.com
My Tweets: http://twitter.com/mickaelistria
Re: Finding difficult to select an context menu [message #1021739 is a reply to message #1021727] Wed, 20 March 2013 15:52 Go to previous messageGo to next message
Craig Foote is currently offline Craig FooteFriend
Messages: 195
Registered: July 2009
Senior Member
Thanks for responding Mickael. Yes I have <useUIThread> set to false. And I am running on windows locally but will build on Hudson eventually with xvnc running. In my pom I have a dependency on 'org.hamcrest' identified. I don't know if I need that or not but I don't know of any implicit dependencies.

Craig
Re: Finding difficult to select an context menu [message #1092315 is a reply to message #1008720] Thu, 22 August 2013 15:58 Go to previous messageGo to next message
Jan Koehnlein is currently offline Jan KoehnleinFriend
Messages: 658
Registered: July 2009
Senior Member
Same here. Context menu in editor doesn't work (widget disposed). Any hints?
Re: Finding difficult to select an context menu [message #1093120 is a reply to message #1092315] Fri, 23 August 2013 15:50 Go to previous messageGo to next message
Mickael Istria is currently offline Mickael IstriaFriend
Messages: 769
Registered: July 2009
Location: Grenoble, France
Senior Member
On 08/22/2013 05:58 PM, Jan Koehnlein wrote:
> Same here. Context menu in editor doesn't work (widget disposed). Any
> hints?

Are you using a recent version of SWTBot? It might be a bug, but these
unit tests seem to work:
*
https://hudson.eclipse.org/hudson/view/SWTBot/job/swtbot-tycho-e4.2/99/testReport/org.eclipse.swtbot.swt.finder.widgets/SWTBotPopupMenuTest/
*
https://hudson.eclipse.org/hudson/view/SWTBot/job/swtbot-tycho-e4.2/99/testReport/org.eclipse.swtbot.eclipse.finder.widgets/SWTBotEclipseProjectTest/

It would be nice if you could reproduce the issue in a simple test case
and contribute it to SWTBot test suite.
--
Mickael Istria
JBoss, by Red Hat
My blog: http://mickaelistria.wordpress.com
My Tweets: http://twitter.com/mickaelistria
Re: Finding difficult to select an context menu [message #1105765 is a reply to message #1093120] Tue, 10 September 2013 08:50 Go to previous messageGo to next message
Jan Koehnlein is currently offline Jan KoehnleinFriend
Messages: 658
Registered: July 2009
Senior Member
I filed https://bugs.eclipse.org/bugs/show_bug.cgi?id=416859

Workaround: Use
new SWTBotMenu(ContextMenuHelper.contextMenu(editor.styledText, "Entry"))
instead of
editor.contextMenu("Entry").click
Re: Finding difficult to select an context menu [message #1105987 is a reply to message #1105765] Tue, 10 September 2013 14:33 Go to previous message
Joey Mink is currently offline Joey MinkFriend
Messages: 87
Registered: July 2009
Location: Centreville, VA, USA
Member

Thanks for sharing the workaround - just made use of it:

SWTBotEclipseEditor editor = bot.activeEditor().toTextEditor();
new SWTBotMenu(ContextMenuHelper.contextMenu(editor.getStyledText(), "New Component Test")).click();
Previous Topic:New Hudson URL for SWTBot
Next Topic:Clicking Source, Design tabs for XML files
Goto Forum:
  


Current Time: Thu Nov 27 13:34:13 GMT 2014

Powered by FUDForum. Page generated in 0.03731 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software