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 SWTBotTreeItem.select() might lead to the following exception:
Caused by: java.lang.IllegalArgumentException: Argument not valid
at org.eclipse.swt.SWT.error(SWT.java:4422)
at org.eclipse.swt.SWT.error(SWT.java:4356)
at org.eclipse.swt.SWT.error(SWT.java:4327)
at org.eclipse.swt.widgets.Widget.error(Widget.java:476)
at org.eclipse.swt.widgets.Tree.setSelection(Tree.java:4986)
at org.eclipse.swt.widgets.Tree.setSelection(Tree.java:4948)
at org.eclipse.swtbot.swt.finder.widgets.SWTBotTreeItem$10.run(SWTBotTreeItem.java:362)
at org.eclipse.swtbot.swt.finder.finders.UIThreadRunnable$4.doRun(UIThreadRunnable.java:196)
at org.eclipse.swtbot.swt.finder.finders.UIThreadRunnable$1.run(UIThreadRunnable.java:89)
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();
tree.setFocus();
final SWTBotTreeItem cssNodeFile = tree.expandNode("application", "css", "bonita_form_confirm.css");
cssNodeFile.contextMenu("Open").click();