Skip to main content
Jump to: navigation, search


< SWTBot
Revision as of 06:12, 4 March 2009 by (Talk | contribs) (Getting started with SWTBot for Eclipse Plugins)

SWTBot Users Guide


SWTBot is an open-source Java based functional testing tool for testing SWT and Eclipse based applications.

SWTBot provides APIs that are simple to read and write. The APIs also hide the complexities involved with SWT and Eclipse. This makes it suitable for functional testing by everyone. SWTBot also provides its own set of assertions that are useful for SWT. You can also use your own assertion framework with SWTBot.

SWTBot can record and playback tests and integrates with Eclipse, and also provides for ant tasks so that you can run your builds from within CruiseControl or any other CI tool that you use.

SWTBot can run on all platforms that SWT runs on. Very few other testing tools provide such a wide variety of platforms.

Quick Start


  • Add the following to your classpath:
  • These are useful if you are using SWTBot to test Eclipse plugins

Getting started with SWTBot

SWTBot requires that tests run on a non-UI thread. If you run tests on the UI thread, they will eventually block the UI at some point in time. More info on this behavior available in the FAQ.

Getting started with SWTBot for SWT applications

Because SWTBot tests need to run in a non-UI thread, it is essential that the application starts off in another thread. The example below uses a simple mechanism to start the tests in another thread. You could use any other mechanism to do this instead.

import net.sf.swtbot.SWTBotTestCase;
import net.sf.swtbot.utils.SWTUtils;
import net.sf.swtbot.widgets.TimeoutException;
import org.eclipse.swt.widgets.Display;
public class FooBarTest extends SWTBotTestCase {
  // pull this up into your own superclass that extends SWTBotTestCase and extend from your superclass instead
  static { 
  protected void setUp() throws Exception {
    waitForDisplayToAppear(5000); // wait for the display to appear before you do anything
  public void testClicksOnAButton() throws Exception {
    bot.button("click me").click();
    bot.button("you just clicked me!").click();
  public void testThisFails() throws Exception {
    bot.button("this does not exist").click();
  private void waitForDisplayToAppear(long timeOut) throws TimeoutException, InterruptedException {
    long endTime = System.currentTimeMillis() + timeOut;
    while (System.currentTimeMillis() < endTime) { // wait until timeout
      try {
        Display display = SWTUtils.display();
        if (display != null)
      } catch (Exception e) {
        // did not find a display? no problems, try again
      Thread.sleep(100); // sleep for a while and try again
    throw new TimeoutException("timed out");
  private static void startApplicationInAnotherThread() {
    new Thread(new Runnable() {
      public void run() {
        new MyApplication().main(new String[] { "some", "command", "line", "arguments for your application" });

Getting started with SWTBot for Eclipse Plugins

Once you've put the above classes on your classpath, you can start using SWTBot. Here's a sample usage of SWTBot:

import net.sf.swtbot.eclipse.finder.SWTBotEclipseTestCase;
// subclassing SWTBotTestCase gives you an instance of
// SWTBot that offers a lot of convinience api
// this class also has a lot of assertions that are suited for ui operations
public class FooTest extends SWTBotEclipseTestCase {
  // stuff you can do with Eclipse
  public void testDoSomethingInterestingWithEclipse() throws Exception {
    bot.view("Package Explorer").close();
    bot.activeEditor().typeText("public static void main ()...");
    bot.activeEditor().quickfix("Rename in file");
    // will insert "System.out.println();" in the currently open editor
    bot.activeEditor().autoCompleteProposal("sys", "sysout - print to standard out");
  // stuff you can do with SWT
  public void testDoSomethingInterestingWithSWT() throws Exception {
    // there are two parts to SWTBot:
    // one to find a control (the subject)
    // and the action to be performed on the control (the verb)"Address Book - Untitled").activate();
    bot.button("Hello World").click();"File").menu("New").click();
    bot.listWithLabel("My Items").select(new String[] { "foo", "bar", "baz" });
    // there are a lot of assertions that are very useful
    assertEnabled(bot.button("Foo Bar"));
    assertVisible(bot.checkBox("This should not visible"));
    assertTextContains("I just love this!", bot.textWithLabel("Comments"));

Executing SWTBot Tests for Eclipse Plugins

Now that you've written the great test that you'd always wanted to, lets now see it run. In order to run the test, right click on the test and select Run As > Run Configurations...


Create a new test under SWTBot Test. Ensure that you're using JUnit 3


Select the application that you want to test


Back to the top