Skip to main content

Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » SWTBot » How to test context or popup Menus for a Composite.
How to test context or popup Menus for a Composite. [message #1700733] Mon, 06 July 2015 17:28
Jordan Deyton is currently offline Jordan DeytonFriend
Messages: 10
Registered: July 2013
Junior Member
Hello SWTBot gurus and fellow users,

Question: I'm wondering if it is possible and, if so, how to test a context (popup, right-click) Menu that is set for a Composite.

So far, I've only seen examples where the context Menu is for an item in a Tree. I've also tried using the built-in context Menu finders with no luck.

I have some sample code below for a simple Composite with a context Menu and SWTBot test code.

Thanks in advance for any pointers, clues, etc.


Example: Here are a couple of test classes to demonstrate the problem.

The Composite being tested. A simple Composite with a context Menu set for it. The Menu is built using a JFace MenuManager.
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;

public class TestComposite extends Composite {

	public boolean menuItemClicked = false;

	public TestComposite(Composite parent) {
		super(parent, SWT.NONE);

		setLayout(new FillLayout());

		final MenuManager menuManager = new MenuManager();
		menuManager.add(new Action("bleh") {
			public void run() {
				menuItemClicked = true;
				System.out.println("bleh clicked...");




The SWTBot test code.
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swtbot.swt.finder.SWTBotTestCase;
import org.eclipse.swtbot.swt.finder.junit.SWTBotJunit4ClassRunner;
import org.eclipse.swtbot.swt.finder.widgets.SWTBotShell;
import org.junit.Test;
import org.junit.runner.RunWith;

public class TestCompositeTester extends SWTBotTestCase {

	private Shell s;
	private TestComposite c;

	public void checkContextMenuItem() {
		final Display display = Display.getDefault();
		display.syncExec(new Runnable() {
			public void run() {
				s = new Shell(display);
				c = new TestComposite(s);

		SWTBotShell botShell = new SWTBotShell(s);


The failure trace when running the SWTBot test code.
org.eclipse.swtbot.swt.finder.widgets.TimeoutException: Timeout after: 5000 ms.: Could not find context menu with text: bleh
	at org.eclipse.swtbot.swt.finder.SWTBotFactory.waitUntil(
	at org.eclipse.swtbot.swt.finder.SWTBotFactory.waitUntil(
	at org.eclipse.swtbot.swt.finder.SWTBotFactory.waitUntil(
	at org.eclipse.swtbot.swt.finder.widgets.AbstractSWTBot.contextMenu(
	at org.eclipse.swtbot.swt.finder.widgets.AbstractSWTBot.contextMenu(
	at sandbox.plugin.TestCompositeTester.checkContextMenuItem(
	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(
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(
	at org.junit.runners.ParentRunner.runLeaf(
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(
	at org.junit.runners.ParentRunner$
	at org.junit.runners.ParentRunner$1.schedule(
	at org.junit.runners.ParentRunner.runChildren(
	at org.junit.runners.ParentRunner.access$000(
	at org.junit.runners.ParentRunner$2.evaluate(
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(
	at org.eclipse.swtbot.eclipse.core.RemotePluginTestRunner.main(
	at org.eclipse.swtbot.eclipse.core.UITestApplication.runTests(
	at org.eclipse.e4.ui.internal.workbench.swt.E4Testable$
	at Source)
Previous Topic:RCP Testing -- Contribution Identities
Next Topic:Problem with Mars on Kali
Goto Forum:

Current Time: Wed May 31 00:38:22 GMT 2023

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

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

Back to the top