Contents

SWTBot Users Guide

Introduction

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

Configuration

Add the following to your classpath:

net.sf.swtbot.finder
org.apache.commons.collections
org.apache.log4j

These are useful if you are using SWTBot to test Eclipse plugins

net.sf.swtbot.eclipse.finder

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.

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:

importnet.sf.swtbot.eclipse.finder.SWTBotTestCase;// 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 operationspublicclass FooTest extends SWTBotTestCase {// stuff you can do with Eclipsepublicvoid testDoSomethingInterestingWithEclipse()throwsException{
bot.view("Package Explorer").close();
bot.editor("HelloWorld.java").save();
bot.editor("FooBar.java").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 SWTpublicvoid testDoSomethingInterestingWithSWT()throwsException{// there are two parts to SWTBot:// one to find a control (the subject)// and the action to be performed on the control (the verb)
bot.shell("Address Book - Untitled").activate();
bot.button("Hello World").click();
bot.menu("File").menu("New").click();
bot.captureScreenshot("myscreenshot.png");
bot.listWithLabel("My Items").select(newString[]{"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"));}}