Framework of the week: SwtBot

Purpose

SwtBot aims to make your Swt (Eclipse) UI testable, it contains many helpers and components to interact with your interface with an automated way.

Concepts

With SwtBot, you’ll manipulate an instance of ‘bot’ which can interact with the environment. SwtBot uses a Fluent api (each mehotd will return the instance of the bot, allowing you to chain calls.
For example, here’s how to click on the ‘File’-> ‘Save’ menu:

bot.menu("File").menu("Save").click();

Swtbot uses a specific Junit runner to bootstrap the bot at startup. Your test must also extends an SwtBot test class to make the bot available:

@RunWith(SWTBotJunit4ClassRunner.class)
public class ClipboardPreferencesTest extends SWTBotEclipseTestCase {
 
 
	@Test
	public void testSave() {bot.menu("File").menu("Save").click();}
}

There are multiple test case abstract classes you can extend: SWTBotEclipseTestCase (Swt tests), AbstractViewpointSwtBotGefTestCase (Sirius specific), SwtBotGeftestCase (for Gef/GMF editors tests).

Getting started

In order to begin, you’ve to install SwtBot in your eclipse (update site).
Then, adding all the SwtBot plugins on your run configuration (you can for example import all *swtbot* plugins in your workspace before running the platform you want to test).

Then, you’ll have to create an SwtBot test plugin: a simple Eclipse plugin
containing these Manifest entries:

 org.eclipse.swtbot.go,
 org.eclipse.swtbot.eclipse.gef.finder

You can now create a simple test as specified below.

Finnally you can launch an SwtBot Test run configuration and check that your code is correct!

Share Button

Leave a Reply

Your email address will not be published. Required fields are marked *