SWTBot/Snippets

From Eclipsepedia

Jump to: navigation, search


SWTBot
Website
Update Sites
Community
Mailing List
Forums/Newsgroups
IRC
Contribute
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­.

ECLIPSE_DIR=/usr/local/eclipse
# ganymede, galileo or helios
REPOSITORY_VERSION=galileo
# Uninstall the feature.
$ECLIPSE_DIR/eclipse -nosplash -consoleLog \
 -application org.eclipse.equinox.p2.director \
 -uninstallIU org.eclipse.swtbot.eclipse.gef.feature.group,org.eclipse.swtbot.eclipse.test.junit4.feature.group \
# (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 http://download.eclipse.org/technology/swtbot/$REPOSITORY_VERSION/dev-build/update-site/ \
 -installIU org.eclipse.swtbot.eclipse.gef.feature.group,org.eclipse.swtbot.eclipse.test.junit4.feature.group \

Note: apparently the application p2.director doesn't exist in eclipse 3.4. The alternative/equivalent is org.eclipse.equinox.p2.director.app.application, 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: http://help.eclipse.org/galileo/index.jsp?topic=/org.eclipse.platform.doc.isv/guide/p2_director.html). 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 LogListener LISTENER = new LogListener();
 
  private static final class LogListener implements ILogListener
  {
    private final List<IStatus> mStatus = new ArrayList<IStatus>();
 
    public void logging(IStatus status, String plugin)
    {
      mStatus.add(status);
    }
  }
 
  @BeforeClass
  public static void registerLog()
  {
    getLog().addLogListener(LISTENER);
  }
 
  @AfterClass
  public static void unregisterLog()
  {
    getLog().removeLogListener(LISTENER);
  }
 
  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.
    clearExceptions();
    // Just to test
    getLog().log(new Status(IStatus.ERROR,
                            "org.eclipse.swtbot",
                            "fake error",
                            new NullPointerException("Fake NPE")));
    // This should be done in a @After or tearDown method.
    checkExceptions();
  } 
 
  public void clearExceptions()
  {
    LISTENER.mStatus.clear();
  }
 
  public 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       
bot.menu("Window").menu("Open Perspective").menu("Other...").click();
SWTBotShell openPerspectiveShell = bot.shell("Open Perspective");
openPerspectiveShell.activate();
 
// select the dialog
bot.table().select("Debug");
bot.button("OK").click();