Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » SWTBot » Cannot use ClickContextMenu on a extended SWTBot widget
Cannot use ClickContextMenu on a extended SWTBot widget [message #1433730] Mon, 29 September 2014 07:20
ju lei is currently offline ju leiFriend
Messages: 5
Registered: September 2014
Location: Beijing
Junior Member
I extends a SWTBot widget according to NatTable as below:



public class SWTBotNatTable extends AbstractSWTBotControl<NatTable> {

SWTBotNatTable natTable;

public SWTBotNatTable(NatTable natTable) {
super(natTable, null);
}

public SWTBotNatTable clickTableHeader(int columnIndex){
return clickCell(0, columnIndex);
}
.......
}


I extends bot as below:

public class BotExt extends SWTBot {

public BotExt() {
super(new ControlFinder(), new MenuFinder());
}

public BotExt(Widget parent) {
super(new ChildrenControlFinder(parent), new MenuFinder());
}
......
}


My ContextMenuHelper class copys from web as below:


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

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.swt.widgets.Widget;
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;

import java.util.Arrays;

public class ContextMenuHelper1 {

public static void clickContextMenu(final AbstractSWTBot<? extends Control> bot, final Matcher<? extends org.eclipse.swt.widgets.Widget>... matchers) {
// show
MenuItem menuItem = null;
try {
menuItem = UIThreadRunnable.syncExec(new WidgetResult<MenuItem>() {
@SuppressWarnings("unchecked")
public MenuItem run() {
MenuItem menuItem = null;
Control control = bot.widget;
Menu menu = control.getMenu(); --- I got a null menu here
for (Matcher<? extends org.eclipse.swt.widgets.Widget> m : matchers) {
Matcher<?> matcher = allOf(instanceOf(MenuItem.class), m);
menuItem = show(menu, matcher);
if (menuItem != null) { --- I got a null menu item here
menu = menuItem.getMenu();
} else {
hide(menu);
throw new WidgetNotFoundException("Could not find " + matcher + " among " + availableItems(menu));
}
}
return menuItem;
}
});
} catch (Exception e) {
if (e.getCause() instanceof WidgetNotFoundException) {
throw new WidgetNotFoundException("Could not find menu: " + Arrays.asList(matchers), e.getCause());
}
}
if (menuItem == null) {
throw new WidgetNotFoundException("Could not find menu: " + Arrays.asList(matchers));
}

// click
click(menuItem);

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

/**
* Clicks the context menu matching the text.
*
* @param text the text on the context menu.
* @throws WidgetNotFoundException if the widget is not found.
*/
@SuppressWarnings("unchecked")
public static void clickContextMenu(final AbstractSWTBot<? extends Control> bot, final String... texts) {
Matcher<? extends Widget>[] matchers = new Matcher[texts.length];
for (int i = 0; i < texts.length; i++) {
matchers[i] = withMnemonic(texts[i]);
}
clickContextMenu(bot, matchers);
}

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;
}

static String availableItems(Menu menu) {
StringBuilder sb = new StringBuilder();
if (menu != null) {
MenuItem[] items = menu.getItems();
for (final MenuItem menuItem : items) {
sb.append(menuItem.getText().replace("&", ""));
sb.append(", ");
}
}
return sb.toString();
}

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);
}
});
}

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


Then I use below sentences and try to click context menu on a natTable:

SWTBotNatTable natTable = new BotExt().getNatTable(0);
natTable.setFocus();
ContextMenuHelper1.clickContextMenu(natTable, "Add to repository context");


But I got below problems:


org.eclipse.swtbot.swt.finder.exceptions.WidgetNotFoundException: Could not find menu: [with mnemonic 'Add to repository context']
at org.talend.swtbot.helpers.ContextMenuHelper1.clickContextMenu(ContextMenuHelper1.java:76)
at org.talend.swtbot.helpers.ContextMenuHelper1.clickContextMenu(ContextMenuHelper1.java:103)
at tosstudio.context.UseContextGloballyTest.useContextGloballyTest(UseContextGloballyTest.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.jbehave.core.steps.StepCreator$ParameterisedStep.perform(StepCreator.java:550)
at org.jbehave.core.embedder.StoryRunner$FineSoFar.run(StoryRunner.java:507)
...


And you can see my natTable in attached pic.


Can you tell me where I am wrong? Embarrassed



  • Attachment: pic.PNG
    (Size: 15.36KB, Downloaded 111 times)
Previous Topic:How to Click on a Label using swtbot ??
Next Topic:testing content assist on a text field
Goto Forum:
  


Current Time: Tue Sep 25 21:11:02 GMT 2018

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

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

Back to the top