javajunitswteclipse-rcpswtbot

Can SWTBot recognize Action Buttons?


I have an RCP Application with a ViewPart that has a toolbar with some Actions on it. These Actions are put on the toolbar by the system as simple Buttons with an icon and a tooltip.

The Action looks like this:

public class MyAction extends Action {

    public static final String TITLE = "My Action Tooltip";

    public MyAction() {
        super(TITLE, Activator.getImageDescriptor("icons/clock_edit.png"));
        setToolTipText(TITLE);
    }

    // ...
}

Now I am trying to invoke a button click on them with SWTBot, like this:

SWTBotButton myButton = bot.buttonWithTooltip(MyAction.TITLE);
myButton.click();

And if I let the SWTBot test run, I get the error message that it couldn't find the Button:

org.eclipse.swtbot.swt.finder.exceptions.WidgetNotFoundException: Could not find widget matching: (of type 'Button' and with tooltip 'My Action Tooltip' and with style 'SWT.PUSH')
    at org.eclipse.swtbot.swt.finder.SWTBotFactory.waitUntilWidgetAppears(SWTBotFactory.java:362)
    at org.eclipse.swtbot.swt.finder.SWTBotFactory.widget(SWTBotFactory.java:309)
    at org.eclipse.swtbot.swt.finder.SWTBot.buttonWithTooltip(SWTBot.java:205)
    at org.eclipse.swtbot.swt.finder.SWTBot.buttonWithTooltip(SWTBot.java:193)

Now I'm wondering, is an Action not put onto the Toolbar as an SWT.PUSH Button? Or what could be the reason that it can't find it?


Solution

  • The Buttons on the Toolbar can be found by SWTBot a bit differently. I was finally able to do that like this:

    List<SWTBotToolbarButton> items = view.getToolbarButtons();
    for (SWTBotToolbarButton button : items) {
        if (MyAction.TITLE.equals(button.getToolTipText())) {
            button.click();
            break;
        }
    }