vogella training Android Training Eclipse Training

SWTBot for testing Eclipse Applications - Tutorial

Lars Vogel

Version 2.2

20.02.2012

Revision History
Revision 0.1 18.09.2010 Lars
Vogel
Created
Revision 0.2 - 2.2 25.09.2010 - 20.02.2012 Lars
Vogel
bug fixes and enhancements

SWTBot

This tutorial describes how to use SWTBot to test Eclipse RCP applications. Within this tutorial Eclipse 3.7 (Indigo) is used.


Table of Contents

1. SWTBot for testing
1.1. SWTBot Overview
1.2. SWTBot API
2. Installation
3. Prerequisites for this tutorial
4. Tutorial: Test Eclipse 3.x RCP applications
4.1. Create project
4.2. Create test plug-in
5. Screenshot
6. Thank you
7. Questions and Discussion
8. Links and Literature
8.1. Source Code
8.2. SWTBot Resources
8.3. vogella Resources

1. SWTBot for testing

1.1. SWTBot Overview

SWTBot is an Eclipse project which allows to test the user interface of an Eclipse or SWT application.

SWTBot provides an API which allows to interact with the user interface of the application and to validate certain conditions of the user interface.

The application is started and controlled by SWTBot.

SWTBot can be used in JUnit Tests. If SWTBot is used to test Eclipse based applications you run the JUnit Test with a "JUnit Plug-in Test" run configuration.

JUnit Plug-in Tests allow to start and test Eclipse bundles.

1.2. SWTBot API

SWTBot is the base class for testing SWT applications.

SWTWorkbenchBot for an Eclipse 3.x RCP test and provides the API interact with Eclipse 3.x applications. The SWTWorkbenchBot class extends SWTBot.

Unfortunately SWTBot does currently not provide a special API for Eclipse 4.

An UI interaction may take some time, e.g. if the application reads some data. Therefore SWTBot will wait a while before throwing an exception. The default value is currently 5 seconds. You can change the timeout via the following:

				
// Set the timeout to 6 seconds
SWTBotPreferences.TIMEOUT = 6000;
			

JUnit does not require that the tests run in the same order as defined. To ensure that the workbench is in the same state for each test you can use:

				
bot.resetWorkbench();
			

2. Installation

Install SWTBot via the Eclipse Update manager . The update site for the SWTBot is http://download.eclipse.org/technology/swtbot/helios/dev-build/update-site/

Install the " SWTBot Eclipse Features " and " SWTBot SWT Features ".

3. Prerequisites for this tutorial

This tutorial assumes what you have basic understanding of development for the Eclipse platform. Please see Eclipse 4 RCP Tutorial , Eclipse RCP Tutorial or Eclipse Plug-in Tutorial if you need any basic information.

4. Tutorial: Test Eclipse 3.x RCP applications

4.1. Create project

Create an new Eclipse RCP application called "de.vogella.swtbot.app" based on the "Eclipse application with a view" template.

Change the class View to the following.

				
package de.vogella.swtbot.app;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.part.ViewPart;

public class View extends ViewPart {
	public void createPartControl(Composite parent) {
		Label label = new Label(parent, SWT.NONE);
		label.setText("My Label");
		Text text = new Text(parent, SWT.SINGLE | SWT.LEAD | SWT.BORDER);
		text.setText("This is my text");

		// Define another text field but also assign an ID to this field for
		// SWTBot
		text = new Text(parent, SWT.SINGLE | SWT.LEAD | SWT.BORDER);
		text.setText("This text has an ID");
		text.setData("org.eclipse.swtbot.widget.key", "text1");

	}

	
/** * Passing the focus request to the viewer's control. */
public void setFocus() { } }

This View defines to text fields, on of them will be identified by SWTBot by the label and the other by a SWTBot specific ID.

4.2. Create test plug-in

Create another Eclipse plug-in called "de.vogella.swtbot.tests".

Add the following dependencies to "de.vogella.swtbot.tests".

  • org.eclipse.ui

  • org.junit4

  • org.hamcrest

  • org.eclipse.swtbot.swt.finder

  • org.eclipse.swtbot.eclipse.finder

Create a new class called "UserInterfaceTester" in the package "de.vogella.swtbot.tests".

Add the following JUnit4 test case.

				
package de.vogella.swtbot.tests;

import static org.junit.Assert.assertTrue;

import org.junit.Test;

public class UserInterfaceTester {
	@Test
	public void test() {
		assertTrue(true);
	}

}

			

Right-click on the test and select Run As SWTBot Test. In the launch configuration maintain your application and add the "de.vogella.swtbot.tests" and "de.vogella.swtbot.app" plugins and their dependencies to the launch configuration.

If everything runs correctly you should get a green test. Change your test() method to the following to do some real testing.

				
package de.vogella.swtbot.tests;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import org.eclipse.swtbot.eclipse.finder.SWTWorkbenchBot;
import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotView;
import org.eclipse.swtbot.swt.finder.widgets.SWTBotText;
import org.junit.Test;

public class UserInterfaceTester {
	private final SWTWorkbenchBot bot = new SWTWorkbenchBot();

	@Test
	public void test() {
		// Find the text after the label "My Label"
		SWTBotText textWithLabel = bot.textWithLabel("My Label");
		// Set the focus and write a text into the text field
		textWithLabel.setFocus();
		assertEquals(textWithLabel.getText(), "This is my text");
		textWithLabel.selectAll();
		textWithLabel.typeText("Java rules them all");
		assertEquals(textWithLabel.getText(), "Java rules them all");

		// Find the text with with the assigned id
		SWTBotText textWithId = bot.textWithId("text1");
		assertEquals(textWithId.getText(), "This text has an ID");

		// Now lets find a view part
		SWTBotView viewById = bot.viewById("de.vogella.swtbot.app.view");
		assertNotNull(viewById);

		// Select the exit menu
		// bot.menu("/File").menu("Exit").click();

		assertTrue(true);
	}
}

			

Run your test again and verify that the test runs ok. Change for example the content of one text field, re-run the test and verify that the test fails.

5. Screenshot

SWTBot can automatically generate screenshots of the running application if a test fails. To use this feature include the plugin "org.eclipse.swtbot.junit4_x" into the dependencies of your test project. Annotate your class test with "@RunWith(SWTBotJunit4ClassRunner.class)". This will automatically generated screenshots in the folder "screenshots" or your test projects. If you do not see this folder in your project try refreshing the project (F5).

You can trigger a screenshot in your code via the method bot.captureScreenshot(fileName).

6. Thank you

Please help me to support this article:

Flattr this

7. Questions and Discussion

Before posting questions, please see the vogella FAQ. If you have questions or find an error in this article please use the www.vogella.com Google Group. I have created a short list how to create good questions which might also help you.

8. Links and Literature

8.1. Source Code

Source Code of Examples

8.2. SWTBot Resources

http://www.eclipse.org/swtbot/ SWTBot FAQ

http://wiki.eclipse.org/SWTBot/FAQ SWTBot FAQ

8.3. vogella Resources

Eclipse RCP Training (German) Eclipse RCP Training with Lars Vogel

Android Tutorial Introduction to Android Programming

GWT Tutorial Program in Java and compile to JavaScript and HTML

Eclipse RCP Tutorial Create native applications in Java

JUnit Tutorial Test your application

Git Tutorial Put everything you have under distributed version control system