Skip to main content
Jump to: navigation, search

Difference between revisions of "SWTBot/UsersGuide"

(imports net.sf --> org.eclipse)
m (Moved advanced usage content to another page)
Line 1: Line 1:
=SWTBot Users Guide=
=SWTBot Users Guide=
Note that this page is for naive and first time users, for advanced usage see
Line 11: Line 16:
==Quick Start==
==Quick Start==
SWTBot comes with a releng project and things generally work out of the box. The build instructions are available in the readme in the releng project.
There are dependencies on org.hamcrest and com.thoughtworks.  These are not checked into the SWTBot repository and nor are they in orbit yet.
You can download the dependencies from for now (and put them in .releng/externals/plugins)
A patched version of the eclipse test framework is available at (you'll need to put this into .releng/externals)
* Add the following to your classpath:
* Add the following to your classpath:
Line 27: Line 23:
* These are useful if you are using SWTBot to test Eclipse plugins
* These are useful if you are using SWTBot to test Eclipse plugins
===Installation from the GIT Repository===
If you expect to be making changes to the SWTBot source code then the you should consider using GIT.  The SWTBot Subversion repository is mirrored to a GIT repository.  Although GIT has a higher learning curve than Subversion, you will find it easier to manage your changes and it will be easier for others to pull your changes.
The GIT repository is at git://
If you clone this repository and import the projects into Eclipse, you will find a few errors.  These can be fixed as follows:
org.eclipse.swtbot.eclipse.ui contains two versions of the .classpath file.  These are named _classpath.eclipse_3.4 and _classpath.eclipse_3.5.  Copy one of these to .classpath, depending on which version of Eclipse is being used in your runtime environment.  The build errors for this plug-in should go away.  There are also two versions of the file.  You will need to copy one of these too if you are going to do PDE builds or create your own update site.
org.eclipse.swtbot.junit4_x contains two versions each of .classpath and MANIFEST.MF in the META-INF directory.  Depending on the version of JUnit you are using, copy one of _classpath.junit4_3 or _classpath.junit4_5 to .classpath and in the META-INF directory copy one of MANIFEST.MF_junit4_3 or MANIFEST.MF_junit4_5 to MANIFEST.MF.  The build errors for this plug-in should go away.
Note that the org.hamcrest and com.thoughtworks bundles are checked into the SWTBot GIT repository and can be found in the org.eclipse.swtbot.releng project in externals/plugins.  So you don't have to fetch these separately as you would if using the Subversion repository at
===Building an SWTBot Update Site===
If you are using SWTBot in a project with many developers, and you want those other developers to run and maintain test, and you have made modifications to SWTBot then you will want to make those modifications easily available to the other developers.  The easiest way to do this is to set up an update site that contains SWTBot with your modifications.
To create an update site, follow the instructions in MakingARelease.HOWTO in the org.eclipse.swtbot.releng bundle.  Do not use the org.eclipse.swtbot.updatesite bundle.  That is old stuff that creates an old-style update site (it predates Equinox p2).  You should ensure unique versions, generally done by including your company name or project name in the version string.
The build.xml file contains the script for building the update site.  However you must first ensure you have all the dependencies.  A separate script is provided to install the dependencies.  This script requires eclipse.sdk.url and eclipse.sdk.archive properties to be set to the location and name of the Eclipse SDK archive file.  The best way to be sure you have all the properties set is to copy to and edit as appropriate.
Once those are set, you can install the dependencies by running:
<code>ant -file download-dependencies.xml download-dependencies</code>
or just run the build.xml script which will run tasks in this script if necessary.
You will need 'unzip' on the command path.  If you are running Windows then you can download a compatible unzip implementation from
===Getting started with SWTBot===
===Getting started with SWTBot===
Line 266: Line 232:

Revision as of 03:04, 1 July 2009

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

SWTBot Users Guide

Note that this page is for naive and first time users, for advanced usage see


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 org.eclipse.swtbot.swt.finder.SWTBotTestCase;
import org.eclipse.swtbot.swt.finder.utils.SWTUtils;
import org.eclipse.swtbot.swt.finder.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

To use SWTBot along with your eclipse plugin application you have to add the below plugins to your dependencies.


Now you can start using SWTBot. Below you can find a sample SWTBot testcase:

import junit.framework.TestCase;
import org.eclipse.swtbot.eclipse.finder.SWTEclipseBot;
import org.eclipse.swtbot.swt.finder.exceptions.WidgetNotFoundException;
import org.eclipse.swtbot.swt.finder.widgets.SWTBotTree;
 * This is a sample swtbot testcase for an eclipse application.
public class TestSampleDialog extends TestCase {
   * In future you will not need to create this instance. 
   * You need to extend SWTBotEclipseTestCase which holds an instance of 
   * SWTEclipseBot.
  protected SWTEclipseBot bot = new SWTEclipseBot();
   * This testcase will create a new java project in 
   * your workspace.
  public void testCreateJavaProject() {
    try {
      bot.view("Welcome").close() ;
    } catch (WidgetNotFoundException e) {
      fail("Welcome window not found.");
    //This will open the the menu File > New > Project wizard"File").menu("New").menu("Project...").click();
    //Select the java project from the wizard tree.
    SWTBotTree projectSelectionTree = bot.tree();"Java Project");
    //Click Next button
    bot.button("Next >").click();
    //'com.swtbot.test.project' is the java project name to create
    bot.textWithLabel("Project name:").setText("com.swtbot.test.project");
    //Click the Finish button
    //Now the project is created in your workspace.
   * This testcase will set the focus on a view.
  public void testFocusView() {
    try {
      bot.view("Welcome").close() ;
    } catch (WidgetNotFoundException e) {
      fail("Welcome window not found.");
    //This will set focus on the Problems view
    //The view is identified using the title of the view.
    //The sleep command can be used to slow down the testcase 
    //execution so that you can see it or you can wait for a
    //background process to complete.

You can find one more SWTBot Testcase snippet below,

// 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