Preamble: This is the first post of the new Eclipse category of BonitaSoft’s community blog. As soon as we find something interesting to talk about when developing Bonita Studio, we will try to share what we learnt with our communities: BonitaSoft and Eclipse. We are also pleased to see this category aggregated to Planet Eclipse knowledge feed. We hope you’ll enjoy reading these posts!
The Bonita Studio developers crew
I found some time recently to take a look at SWTBot to check whether we should use it while developing Bonita Studio (part of Bonita Open Solution, based on Eclipse Gelileo 3.5.1). After a few minutes of reading wiki pages and trying it, I was convinced that SWTBot (and its GEF extension, that also works for GMF) is a must-use project for anyone who has ever it found difficult and time-consuming to write plugin tests.
Only a few hours later our continuous integration build welcomed our first SWTBot based test!
Here is the step-by-step of this awesome encounter:
- Install SWTBot on your development platform by installing SWTBot from updatesite: http://download.eclipse.org/technology/swtbot/galileo/dev-build/update-site
- Create a new plugin to host your test. Add these dependencies to your plugin.

- Write your test
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27package org.bonitasoft.diagram.test;
import org.bonitasoft.studio.model.process.diagram.part.ProcessDiagramEditor;
import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotEditor;
import org.eclipse.swtbot.eclipse.gef.finder.SWTBotGefTestCase;
import org.eclipse.swtbot.swt.finder.junit.SWTBotJunit4ClassRunner;
import org.eclipse.swtbot.swt.finder.widgets.SWTBotMenu;
import org.eclipse.ui.IEditorPart;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
/**
* @author Mickael Istria
*
*/
@RunWith(SWTBotJunit4ClassRunner.class)
public class DiagramTests extends SWTBotGefTestCase {
@Test
public void testDiagramTest() throws Exception {
SWTBotMenu menu = bot.menu("Process");
menu.menu("New").click(); // simulate a click on Process > New menu entry
SWTBotEditor botEditor = bot.activeEditor();
IEditorPart editor = botEditor.getReference().getEditor(false);
Assert.assertTrue("New process should open a process editor", editor instanceof ProcessDiagramEditor);
}
} - Configure your test run
- Use JUnit4 as launcher, and don’t use UIThread (SWTBot tests won’t run in a UIThread)

- Set the product you want to test (leave default org.eclipse.platform.ide for “simple” plugins)

- Increase memory and set a language

- Use JUnit4 as launcher, and don’t use UIThread (SWTBot tests won’t run in a UIThread)
- Run and enjoy the high code coverage that you get with so few lines of code!
- Ok, now let’s try the GEF extension of SWTBot to check some tricky behavior in a diagram editor. This test creates a new process and then activates a tool in the design palette to create a new step, and then does some checks (and all in about a dozen lines
:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18@RunWith(SWTBotJunit4ClassRunner.class)
public class DiagramTests extends SWTBotGefTestCase {
@Test
public void testDiagramTest() throws ExecutionException {
SWTBotMenu menu = bot.menu("Process");
menu.menu("New").click();
SWTBotEditor botEditor = bot.activeEditor();
SWTBotGefEditor gmfEditor = bot.gefEditor(botEditor.getTitle());
gmfEditor.activateTool("Step");
gmfEditor.mouseMoveLeftClick(200, 200);
menu.menu("Save").click();
IGraphicalEditPart part = (IGraphicalEditPart)gmfEditor.mainEditPart().part();
MainProcess model = (MainProcess)part.resolveSemanticElement();
Pool pool = (Pool)model.getElements().get(0);
Assert.assertEquals("Pool should contain 3 nodes", 3, pool.getElements().size());
}
} - Run again, and enjoy even more: SWTBot for GEF provides a lot of very high-level Methods to manipulate your diagram. Without it, writing tests for a GMF based editor was quite difficult, and did not mimic user actions very well. With this, you can test real usage scenarios with very little code.
- The return on investment with SWTBot looks very good, so let’s adopt it and automate test execution in a continuous integration build, leveraging the SWTBot headless framework. The following requires you to be familiar with automated PDE or RCP build and testing.
- Install swtbot in the platform you use to build your plugins or your RCP app. For instance, you can use the P2 director commandline application to install it from updatesite:
java -jar plugins/org.eclipse.equinox.launcher_1.0.201.R35x_v20090715.jar -application org.eclipse.equinox.p2.director -artifactRepository http://download.eclipse.org/technology/swtbot/galileo/dev-build/update-site -metadataRepository http://download.eclipse.org/technology/swtbot/galileo/dev-build/update-site -installIU org.eclipse.swtbot.eclipse.feature.group -installIU org.eclipse.swtbot.eclipse.gef.feature.group -consoleLog
- Install SWTBot headless test framework in your build platform: Download it from SWTBot download page, and expand it in your build platform directory.
- Add the SWTBot runtime and headless plugins and your new test plugin to your test feature.

In our case, we prefered keeping only the org.eclipse.ant.optional.junit fragment and using new junit bundles to avoid conflicts between classes from org.junit and org.junit4 bundles. However, SWTBot provides some alternative fragments to support either junit3 or junit4 if you prefer.You can add the following entries in your map file for new junit bundles:!** Use newer JUnit as described in http://wiki.eclipse.org/Eclipse/Testing/JUnit4_Changes
!** Should facilitate integration with SWTBot headless
plugin@org.junit,4.8.1=GET,http://download.eclipse.org/tools/orbit/downloads/drops/S20100120144102/bundles/org.junit_4.8.1.v4_8_1_v20100114-1600.zip,unpack=true
plugin@org.junit4=v20100104,:pserver:anonymous:@dev.eclipse.org:/cvsroot/eclipse,,org.junit4
plugin@org.eclipse.jdt.junit.runtime=v20091201-0800,:pserver:anonymous:@dev.eclipse.org:/cvsroot/eclipse,,org.eclipse.jdt.junit.runtime - In the piece of script that runs your test, add the following test invocation command:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17<echo>SWTBot test</echo>
<java dir="${eclipse.test.home}" fork="true" output="${eclipse.test.home}/output.txt" logError="true"
classname="org.eclipse.core.launcher.Main" failonerror="false">
<classpath>
<fileset dir="${eclipse.test.home}/plugins">
<include name="org.eclipse.equinox.launcher_*.jar"/>
</fileset>
</classpath>
<arg line="-application org.eclipse.swtbot.eclipse.junit4.headless.swtbottestapplication"/>
<arg line="-testPluginName org.bonitasoft.studio.diagram.test"/>
<arg line="-testApplication org.bonitasoft.studio.application.application"/>
<arg line="-className org.bonitasoft.studio.diagram.test.DiagramTests"/>
<arg line="formatter=org.apache.tools.ant.taskdefs.optional.junit.XMLJUnitResultFormatter,junit-results.xml"/>
<arg line="-nl fr"/>
<arg line="-consoleLog"/>
<jvmarg line="-Xms40m -Xmx348m -XX:MaxPermSize=256m -XX:+HeapDumpOnOutOfMemoryError"/>
</java>
- Install swtbot in the platform you use to build your plugins or your RCP app. For instance, you can use the P2 director commandline application to install it from updatesite:
- At this point, when everything is working, you should be one of the happiest people in the world: You have not only reduced the difficulty and cost of writing tests, but also increased the coverage and the realism of your tests
Congrats and thanks to SWTBot developers for making this possible! We love playing with it when developing Bonita Open Solution!
If someone has a better solution for this integration, please tell me!

#1 by David Green on February 2nd, 2010
Glad to hear that you’re enjoying SWTBot. We’ve been using SWTBot for some time. It’s reassuring to know that every CI build of our Eclipse plug-ins are smoke-tested.
The biggest issue we’ve had to deal with is editors that do Dislpay.asyncExec(). This can cause timing issues with SWTBot tests that cause them to fail intermittently. We’ve eradicated such code from our bundles, resulting in much more stable builds.
#2 by Chris Aniszczyk on February 3rd, 2010
Great post on SWTBot. Once you use it, it’s hard to go back.
In the new edition of the RCP Book, there will be a chapter regarding testing and how to use SWTBot in your application. It should go along way to make people aware of the project and introduce them to some of the SWTBot concepts.
Pingback: Me, my blog and Bonita « Scratsh's Blog
Pingback: MyEclipseCon Wednesday « Oisin Hurley's Weblog
#3 by Ketan Padegaonkar on March 24th, 2010
Could you add this blog entry to http://wiki.eclipse.org/SWTBot/Articles so people can look up your blog entry from there ?
#4 by mickael.istria on March 24th, 2010
@Ketan: Done!
#5 by Xiao on July 4th, 2011
Hi,good article. But when I was running the SWTBot plugin with ‘ant -buildfile build.xml’ in a command line window, I got an error saying :
Could not find plugin “com.ibm.biginsights.swtbot”. But I could get successful result if I run the build.xml in Eclipse. Do you have any suggestion? Thanks a lot!
Here is the detailed error:
——————-
java-test:
[echo] ======================================
[echo] Running swtbot junit tests in com.ibm.biginsights.swtbot.TestSWTBot
[echo] Command line args are -application org.eclipse.swtbot.eclipse.junit4.headless.swtbottestapplication -data workspace_temp
formatter=org.apache.tools.ant.taskdefs.optional.junit.XMLJUnitResultFormatter,D:/installed/eclipse-SDK-3.6.2-win32/eclipse/com.ibm.biginsights.swtbo
t.TestSWTBot.xml formatter=org.apache.tools.ant.taskdefs.optional.junit.PlainJUnitResultFormatter -testPluginName com.ibm.biginsights.swtbot -cl
assName com.ibm.biginsights.swtbot.TestSWTBot -os win32 -ws win32 -arch x86 -consoleLog -debug
[echo] JVM args are -Xms128M -XX:MaxPermSize=512m -Xmx512M
[echo] JUnit Result File: D:/installed/eclipse-SDK-3.6.2-win32/eclipse/results/com.ibm.biginsights.swtbot.TestSWTBot.xml.
[echo] Console output File: D:/installed/eclipse-SDK-3.6.2-win32/eclipse/results/com.ibm.biginsights.swtbot.TestSWTBot.txt.
[echo] ======================================
[java] java.lang.Exception: Could not find plugin “com.ibm.biginsights.swtbot”
[java] at org.eclipse.swtbot.eclipse.junit4.headless.EclipseTestRunner.loadSuiteClass(EclipseTestRunner.java:303)
[java] at org.eclipse.swtbot.eclipse.junit4.headless.EclipseTestRunner.getTest(EclipseTestRunner.java:239)
[java] at org.eclipse.swtbot.eclipse.junit4.headless.EclipseTestRunner.(EclipseTestRunner.java:222)
[java] at org.eclipse.swtbot.eclipse.junit4.headless.EclipseTestRunner.run(EclipseTestRunner.java:206)
[java] at org.eclipse.swtbot.eclipse.junit4.headless.UITestApplication.runTests(UITestApplication.java:116)
[java] at org.eclipse.ui.internal.testing.WorkbenchTestable$1.run(WorkbenchTestable.java:71)
[java] at java.lang.Thread.run(Thread.java:595)
[echo] ======================================
[echo] If you see errors above please see the file D:/installed/eclipse-SDK-3.6.2-win32/eclipse/results/com.ibm.biginsights.swtbot.TestSWTBot.txt
for more information.
[echo] Errors are generally caused by missing or incorrect dependencies.
[echo] ======================================
———————-