Skip to main content

Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » SWTBot » Problem clicking popup menu (stays visible)
Problem clicking popup menu (stays visible) [message #551300] Thu, 05 August 2010 20:15
Alexander Nyssen is currently offline Alexander NyssenFriend
Messages: 244
Registered: July 2009
Location: L√ľnen
Senior Member

I am running into trouble when trying to click a popup menu within an SWTBot test. I am able to find the popup menu via EventContextMenuFinder and I am also able to click the menu (i.e. the SWT.Selection event arrives at the menu). However, the menu is not hidden (and thus the thread showing the popup remains in its readAndDispatch loop). I have created a test case to reproduce my problem (see below).

An additional note: on MacOSX I was able to let the popup disappear by sending a keyboard event to the shell after having done the click (new SWTWorkbenchBot().activeShell().pressShortcut(Keystrokes.ESC )Wink, on Linux GTK not even this worked. Any ideas?


package org.mechatronicmodeler.ui.editors.tests.utils;

import static org.eclipse.swtbot.swt.finder.matchers.WidgetMatcherFactory. withMnemonic;

import java.util.List;

import junit.framework.TestCase;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swtbot.swt.finder.exceptions.WidgetNotFoundExcep tion;
import org.eclipse.swtbot.swt.finder.finders.EventContextMenuFinder ;
import org.eclipse.swtbot.swt.finder.finders.UIThreadRunnable;
import org.eclipse.swtbot.swt.finder.results.VoidResult;
import org.eclipse.swtbot.swt.finder.utils.SWTUtils;
import org.eclipse.swtbot.swt.finder.widgets.SWTBotMenu;
import org.hamcrest.Matcher;

public class PopupTestCase extends TestCase {

public class PopupHolder {

public void open() {
final Menu menu = new Menu(new Shell(SWTUtils.display()), SWT.POP_UP);

final MenuItem menuItem1 = new MenuItem(menu, SWT.PUSH);
menuItem1.addListener(SWT.Selection, new Listener() {
public void handleEvent(final Event e) {

final MenuItem menuItem2 = new MenuItem(menu, SWT.PUSH);
menuItem2.addListener(SWT.Selection, new Listener() {
public void handleEvent(final Event e) {

final MenuItem menuItem = new MenuItem(menu, SWT.CASCADE);
final Menu subMenu = new Menu(menu.getShell(), SWT.DROP_DOWN);
for (int i = 0; i < 5; i++) {
final MenuItem subMenuItem = new MenuItem(subMenu, SWT.CASCADE);
final String menuText = "3." + (i + 1);
subMenuItem.addListener(SWT.Selection, new Listener() {

public void handleEvent(final Event e) {


final Display display = menu.getDisplay();
while (!menu.isDisposed() && menu.isVisible()) {
if (!display.readAndDispatch()) {

public void testClickPopup(){
MenuItem menuItem = null;
EventContextMenuFinder finder = new EventContextMenuFinder();
try {

// show popup
UIThreadRunnable.asyncExec(new VoidResult() {
public void run() {
new PopupHolder().open();


Matcher<MenuItem> withMnemonic = withMnemonic("1");
List<MenuItem> menus = finder.findMenus(withMnemonic);
if (menus.isEmpty())
throw new WidgetNotFoundException("Could not find a menu item");
menuItem = menus.get(0);

} finally {


new SWTBotMenu(menuItem).click();

// wait here just to observe that menu is not closed
try {
} catch (InterruptedException e) {

protected static void waitForSync() {
UIThreadRunnable.syncExec(new VoidResult() {
public void run() {
// wait for sync
Previous Topic:How to check if a Text() is read only
Next Topic:Delete a particular line or section of code in editor
Goto Forum:

Current Time: Thu Jan 27 21:50:53 GMT 2022

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

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

Back to the top