Little trick with SWTBotTree manipulation if it returns an IllegalArgumentException

aurelien.pupier's picture
Blog Categories: 

I came across a strange issue with manipulating SWTBotTree, and it took me few hours to find a workaround. I found no exact match with my issue during my research, so let's keep a trace here while waiting for a better understanding of the issue and a fix in SWTBot.

The issue

In a SWTBot Test, calls to SWTBotTreeItem.contextMenu() or might lead to the following exception:

Caused by: java.lang.IllegalArgumentException: Argument not valid
    at org.eclipse.swt.SWT.error(
    at org.eclipse.swt.SWT.error(
    at org.eclipse.swt.SWT.error(
    at org.eclipse.swt.widgets.Widget.error(
    at org.eclipse.swt.widgets.Tree.setSelection(
    at org.eclipse.swt.widgets.Tree.setSelection(
    at org.eclipse.swtbot.swt.finder.widgets.SWTBotTreeItem$
    at org.eclipse.swtbot.swt.finder.finders.UIThreadRunnable$4.doRun(
    at org.eclipse.swtbot.swt.finder.finders.UIThreadRunnable$

Quite an obscure message... what is happening? How can I solve my issue?

The workaround

Easily applicable - but hard to guess - the workaround consists of simply calling SWTBotTree.setFocus() before manipulating the SWTBotTree. For instance:

final SWTBotTree tree = bot.tree();
final SWTBotTreeItem cssNodeFile = tree.expandNode("application", "css", "bonita_form_confirm.css");

Technical details

If we look closer in Tree.setSelection method , the exception is due to a disposed TreeItem:

TreeItem item = items [0];
if (item != null) {
        if (item.isDisposed ()) error (SWT.ERROR_INVALID_ARGUMENT);

The thing which is strange is that the SWTBotTreeItem is calling setFocus before calling the setSelection, as you can see here in

public SWTBotTreeItem select() {
                syncExec(new VoidResult() {
                        public void run() {
                return this;

If someone understands the issue better than me, please share your ideas in the bug report: