Abstract:
This article is for anyone who wants to automate Eclipse-based UI testing seamlessly across all platforms.This article has 3 parts which will help even a beginner to easily adopt to SWTBot as a testing tool. Part1 - Installing SWTBot gives detailed steps for setting up SWTBot on Eclipse v4.2.2 , Part2 - Getting Started with SWTBot has the guide to begin UI automation with SWTBot with the assistance of SWTBot Recorder and Generator utility .In the final part, we will learn about techniques of creating a reusable automation framework methods.
What is SWTBot?
SWTBot is an open-source Java based UI functional testing tool for testing SWT, Eclipse and GEF based applications. It is eclipse based plug-in with rich set of libraries for automating all UI actions on Eclipse IDE. The SWTBot APIs are Java based and quite simple to use therefore the learning curve is quick. SWTBot has been adopted as the automation tool for InfoSphere BigInsights Eclipse based tooling plug-in .
SWTBot is supported on all platforms that Eclipse runs on , therefore it supports 32 and 64 bit versions of Windows , Linux and MacOS . Some of the benefits of using SWTBot are :
-
SWTBot provides APIs that are simple to use and are extensible.
-
Don’t have to build Object mapping, as it hides the complexities involved with SWT and Eclipse.
-
Suitable for Developers and Testers to write test cases quickly.
-
Test Recorder and Generator utility generates the java code using SWTBot APIs which are readily consumable as test cases.
-
Supports integration with Ant for headless testing
"Please do note that SWTBot can only automate the tests within the Eclipse Workbench GUI and does not support web based browser automation. Also SWTBot does not recognize Java Swing objects like graphs or charts that are not built with Standard Widget Toolkit (SWT) widgets."
Software Prerequisites:
Eclipse v4.2.2 or above
SWTBot v2.2.0 or above
JDK 1.6 or above
Hardware Requirements:
Windows 7
Linux
MacOS
Part1 - Installing SWTBot
Download the repository.zip file from http://download.eclipse.org/technology/swtbot/releases/2.2.0/repository.zip and install the SWTBot plugin on your eclipse with the archive option. Alternatively you can use the update site http://download.eclipse.org/technology/swtbot/releases/2.2.0/ as well.
Install the SWTBot plug-in with the following steps:
-
Go To Help menu Install New Software…
-
Click on Add and click on Archive... to add new software repository by pointing to the repository.zip downloaded.
Figure 1 - Install SWTBot plugin in Eclipse

-
Select all SWTBot components and click Next and proceed with the Install and ensure that Eclipse is restarted.Note: Make sure the checkbox 'Contact all update sites during install to find required software' is selected to fetch the required plugins.
Figure 2 - Install SWTBot plugin in Eclipse

Part2 - Getting Started with SWTBot
Before you start adding your test case, you must have exposure to JUnit testing framework or at least basic understanding of Object Oriented Programming, Java, and JUnit Framework which will be used extensively in SWTBot.
The SWTBot test cases are very similar to JUnit tests except that the SWTBotJunit4ClassRunner will be used for running the SWTBot tests instead of Standard TestRunner for JUnits. SWTBot tests have its own launch configuration very much similar to JUnit tests launch configuration.
Skeleton of SWTBot Test Case
A simple test case will look like this
@RunWith(SWTBotJunit4ClassRunner.class)
public class FooTest extends BaseTestCase {
@Test
public void canSendEmail() {
}
}
As you can see, it is quite similar to adding Junits with only exception is that it has a Custom Junit4 Class Runner - SWTBotJunit4ClassRunner which is included in the SWTBot plugin.
public class BaseTestCase {
@Before
public void setup() {
Perspective.set("BigInsights");
}
@After
public void tearDown() {
Perspective.reset("BigInsights");
}
}
Step by Step guide for creating your 1st test case with SWTBot:
-
To begin with create a new Plug-in project say Automation and ensure that all properties are checked in the 2nd page as show in Figure 4.
Figure 3 - Creating new Plugin Project

Figure 4 - Creating new Plugin project

-
Open the MANIFEST.MF from META-INF directory in the new project. Go to Dependencies tab and add the SWTBot and org.junit Plug-ins to Required Plug-ins.
Note: org.eclipse.ui and org.eclipse.core.runtime are added by default for Plug-in projects which contributes to UI. Any recent version of org.junit or org.junit4 can be added.
Figure 5 - Adding SWTBot Plugin dependencies

-
Now Add a Java Class say ‘FirstClass under the package ‘automation’ which contains the Activator.java and add the following annotations. The RunWith annotation will indicate that tests are loaded using SWTBotJunit4ClassRunner the default runner for SWTBot tests.
@RunWith(SWTBotJunit4ClassRunner.class)
public class FirstClass{
private static SWTWorkbenchBot bot = new SWTWorkbenchBot();
}
-
Now go to FileNewSWTBotRun Test Recorder to launch the SWTBot Test recorder and select type as JDT Dialog in Recorder dialog as shown in screen captures below (Figure 6 ,7 & 8).
Figure 6 - Open SWTBot Test Recorder

Figure 7 - Open SWTBot Test Recorder Type and Eclipse instance

This will open a launch configuration dialog as shown below in Figure 8 ,which will have the Eclipse launch configuration parameters such ‘Workspace Data’ with the default location – ‘${workspace_loc}/../runtime-TestRecorderJDTDialog’. In the ‘Programs To Run’ option select ‘Run a Product’ and set it to “org.eclipse.platform.ide” and Run to launch the Eclipse that shall be used for recording.
Figure 8 - Test Recorder JDT Dialog Configuration

-
Once the JDT Recorder launches the new Eclipse workbench, click on Add Method and then start recording your test. Click on Pause once you have completed and then copy the test method which is generated to the FirstClass and add the annotaion @Test before the createProject() test.
Figure 9 - SWTBot Test Recorder

-
Now the test method will look similar to snippet below:
Please note the bot.sleep() methods are interleaved manually for stability of the execution. Now your first SWTBot test is ready for launch.
Figure 10 - A test case with SWTBot Recorder and Generator Utility

-
To launch the test, Right the class from Project Explorer and select Run As > SWTBot Test which would launch a new eclipse workbench to run the tests. See screen capture below with a simple SWTBot test case launched.(Figure 11)
Figure 11 - SWTBot test run launched

Part3 - Building Robust and Reusable Automation Framework:
Now that you have learnt to create a simple test case using SWTBot Recorder and Generator facility, lets delve into guiding principles for creating a reliable and robust automation framework.
Step 1 - Stabilize the Generated Code:
The SWTBot Recorder and Generator has done its job by recording UI actions in the sequence and it is now time for the us to improve the stability of the code and add additional framework methods for validation. Although the recorder code captures all the actions during recording, it does not record the interval of time we wait for an event to complete and does not add validation check points on its own. Therefore, it becomes the onus of the test case developer to enforce these checks. Hence, it is important to understand the use bot.sleep() and bot.waitUntil() methods which will serve this purpose.
Static Sleep : bot.sleep()
When bot.sleep(X) is used the test runner SWTBot to waits explicitly for X milli seconds before jumping to the next line of code. Adding bot.sleep() is recommended when the UI response time is predictable. For example , open Perspective or opening Problems View should not take more than 2 or 3 seconds .
Dynamic Wait: bot.waitUntil()
When the response time is not fixed or predictable, it is recommended to add a dynamic wait condition with the bot.waitUntil() method which gives the user the flexibility of waiting until a favorable event . For instance time taken for building the entire workspace or running the java project can not be predetermined.
In the code snippet shown below, we create a new Project and we wait until the Create New Project dialog closes by using
bot.waitUntil(shellCloses(activeShell),20000,1000);
This means that the test runner waits until a active shell ( eclipse dialog is referred to as Shell in SWTBot) closes and it checks the state of the shell every 1 second for a maximum of upto a maximum of 20 seconds and throws assertion error if not completed within 20 seconds.
If instead the code was
bot.waitUntil(shellCloses(activeShell));
This will make the SWTBot test runner wait only for time duration set by SWTBotPreferences.TIMEOUT which is not recommended.
Figure 12 - Code Snippet Illustrating bot.waitUntil

Step 2 - Validation Checks:
There are several ways of validating a test. SWTBot in general offers methods that checks that Status of the widget using methods like isEnabled(),isVisible(), isActive(), isChecked() for Check boxes only and isSelected() for Radio Buttons only.
Beyond these SWTBot methods, it is also possible to complement programmatically evaluate the outcome for functionality testing.
For example, if you want to test Build Automatically functionality it is not possible to use isChecked() method in this case since it is not a CheckBox but a MenuItem , so it is not possible to verify by visual cue or object property. However, you can test it by creating a Java Project and adding a Java source code and save it, it is possible to check the list of files found under the bin folder of the project in the Navigator View and check that the corresponding class files are generated and are visible.
Step 3 - Creating Reusable Framework
Once you stabilized the above code with Steps1 & 2, next step is to modularize small functional blocks as much as possible that can serve as a Framework method , which can be later be reused for other tests. For example, creating projects or deleting projects can be added as methods under a Framework class say ProjectUtil and add any new method that you may encounter for other test cases to it. While adding methods to Framework class make sure that it always throws Exception . By this way, the test cases consuming the framework methods can be placed within try block and catch any exception to fail the test with the catch block with JUnit asserts.
It is a good practice to create framework methods such that JUnit test case is devoid of any direct invocation of SWTBot APIs .By doing this, If there are minor UI changes for functionality, it is easier to change the framework method alone and all the test cases using that framework need not be modified.
Figure 13 - Figure illustrating a transformed Framework method

Figure 14 - A transformed Test Case calling the Framework method within try catch block

Best Practices For Building Test Automation Framework Using SWTBot:
-
Create a BaseTestCase class with the setup() and tearDown() methods that shall have all common set of steps that you would like to have for each test case.
-
All individual test classes can extend the BaseTestCase and have additional annotated methods @BeforeClass and @AfterClass as needed.
-
Group all your test cases in different test classes based on their functionality which will make a independent Function Test Suite by itself.
-
Avoid hard coding any test-data instead use parameters where ever possible
-
Make good use of bot.sleep() or bot.waitUntil() for avoiding timing related issues which is predominant with GUI automation.
References:
-
http://junit.org/
-
https://wiki.eclipse.org/SWTBot
-
http://download.eclipse.org/technology/swtbot/helios/dev-build/apidocs/
-
https://wiki.eclipse.org/SWTBot/FAQ
Disclaimer:
“The postings on this site are my own and don’t necessarily represent IBM’s positions, strategies or opinions.”
Tags: 
testing
automation
swtbot
eclipse