Skip to main content

Notice: This Wiki is now read only and edits are no longer possible. Please see: for the plan.

Jump to: navigation, search


Update Sites
Mailing List
Open Bugzilla tickets
Open Gerrit reviews
Browse Source
Continuous Integration

Some snippets for working with SWTBot

Installing/Updating on the command line

Here is a little bash script to help manage SWTBot's version on the command line. Useful for managing an Eclipse installation used for Continuous Integration. It is by no means perfect and you will need to set the proper version for the equinox launcher and the swtbot repository (ganymede, galileo or helios). Feel free to modify to fit your needs­.

# ganymede, galileo or helios
# Uninstall the feature.
$ECLIPSE_DIR/eclipse -nosplash -consoleLog \
 -application org.eclipse.equinox.p2.director \
 -uninstallIU, \
# (Re)Install the feature. This is how you update in the p2 world.
$ECLIPSE_DIR/eclipse -nosplash -consoleLog \
 -application org.eclipse.equinox.p2.director \
 -repository$REPOSITORY_VERSION/dev-build/update-site/ \
 -installIU, \

Note: apparently the application p2.director doesn't exist in eclipse 3.4. The alternative/equivalent is, but this is an undocumented feature. Another workaround would be to use an eclipse 3.5 to manage an eclipse 3.4, which sounds like the way to go (it's even documented: All you need to do is add -destination eclipse/3.4/path -profile PROFILE when installing, where PROFILE is defined by the property eclipse.p2.profile in the config.ini (configuration/config.ini) of the destination.

Failing from exception in another thread

Most of the time, when you fiddle with the UI, things under the hood might crash and throw exceptions. These exceptions are then catched by Eclipse and logged somewhere so the end-user/developer can have a clue of what went wrong. But when you write UI tests, these exception are not readily available to you and things might falsely succeed or fail with a weird error. So, to have a better fail message, I present you with this little snippet!

public class Logger
  private static final ILogListener LISTENER = new ILogListener()
    private final List<IStatus> mStatus = new ArrayList<IStatus>();
    public void logging(IStatus status, String plugin)
  public static void registerLog()
  public static void unregisterLog()
  public static ILog getLog()
    return Platform.getLog(Platform.getBundle("org.eclipse.swtbot"));
  // A small test case to proof that it is working
  @Test(expected = NullPointerException.class)
  public void testLogging() throws Throwable
    // This should be done in a @Before or setUp method. To make sure that
    // errors that happened in the other tests don't pollute this test.
    // Just to test
    getLog().log(new Status(IStatus.ERROR,
                            "fake error",
                            new NullPointerException("Fake NPE")));
    // This should be done in a @After or tearDown method.
  public static void clearExceptions()
  public static void checkExceptions() throws Throwable
    for (IStatus status : LISTENER.mStatus)
      if (status.getException() != null)
        // Since it's a list, the first exception that gets logged will be thrown.
        throw status.getException();

Switch perspective

This allows you to switch perspectives in eclipse from within the Window>Open Perspective>Other... menu.

// Change the perspective via the Open Perspective dialog"Window").menu("Open Perspective").menu("Other...").click();
SWTBotShell openPerspectiveShell ="Open Perspective");
// select the dialog

Tree Expansion

Expansion of items in a SWTBotTree may be tricky, especially if the tree is populated lazily. If a tree item with no children (yet) is expanded, any children added after the expansion will not be seen by SWTBot. You need to collapse and re-expand the item in order to see the new items. A robust solution is to create a wait class that does this automatically. The following snippet shows how to implement such a class:

class WaitForItems extends DefaultCondition {
  private SWTBotTreeItem item;
  public WaitForItems(SWTBotTreeItem theItem) {
    item = theItem;
  public boolean test() throws Exception {
    if (
    return item.getItems().length > 0;

Back to the top