Difference between revisions of "SWTBot/UsersGuide"

From Eclipsepedia

Jump to: navigation, search
(Getting started with examples)
m (Recorder and Generator: should be a main heading so that it don't end up under GEF in the TOC.)
 
(One intermediate revision by one user not shown)
Line 125: Line 125:
  
 
Example GEF project: http://git.eclipse.org/c/swtbot/org.eclipse.swtbot.git/tree/examples/gef/org.eclipse.gef.examples.logic
 
Example GEF project: http://git.eclipse.org/c/swtbot/org.eclipse.swtbot.git/tree/examples/gef/org.eclipse.gef.examples.logic
 +
<br/>
 
Example SWTBot for GEF test case: http://git.eclipse.org/c/swtbot/org.eclipse.swtbot.git/tree/examples/gef/org.eclipse.gef.examples.logic.test
 
Example SWTBot for GEF test case: http://git.eclipse.org/c/swtbot/org.eclipse.swtbot.git/tree/examples/gef/org.eclipse.gef.examples.logic.test
  
Line 165: Line 166:
 
TODO: create an example that ensure that creation of a specific element is not possible on mainEditPart
 
TODO: create an example that ensure that creation of a specific element is not possible on mainEditPart
  
== Recorder and Generator ==
+
= Recorder and Generator =
  
 
SWTBot comes with a tool that generates some code based on events performed at runtime. It makes writing tests easier and faster. See [[SWTBot/Generator]].
 
SWTBot comes with a tool that generates some code based on events performed at runtime. It makes writing tests easier and faster. See [[SWTBot/Generator]].
  
 
[[Category:Draft Documentation]] [[Category:SWTBot]]
 
[[Category:Draft Documentation]] [[Category:SWTBot]]

Latest revision as of 08:30, 17 October 2013


SWTBot
Website
Update Sites
Community
Mailing List
Forums/Newsgroups
IRC
Contribute
Open Bugzilla tickets
Open Gerrit reviews
Browse Source
Continuous Integration


Contents

[edit] SWTBot Users Guide

Information on this page may be outdated.

Note that this page is for first time users. Advanced Users click here.

[edit] 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.

[edit] Quick Start

[edit] A Screencast

Videos speak louder than pictures and words put together:

[edit] Creating A Project

Create a new project by clicking on File>New>Project. On the New Project Dialog, search for "plug-in", select New Plug-in Project and click Next. Create a new plugin project named org.eclipsecon.swtbot.example.

Swtbot-create-project.gif

[edit] Configuration

  • Add the following to your classpath:
  org.eclipse.swtbot.eclipse.finder
  org.eclipse.swtbot.junit4_x
  org.hamcrest.core
  org.apache.commons.collections
  org.junit4
  org.apache.log4j
  org.eclipse.ui

Swtbot-setup-dependencies.gif

[edit] Getting started with SWTBot

SWTBot requires that tests run on a non-UI thread, so that PlatformUI.getWorkbench() will return you null and that traditional unit-test code won't work. If you run tests on the UI thread, they will eventually block the UI at some point in time. Take a look into the FAQ for explanations and workaround.

[edit] Getting started with SWTBot for Eclipse Plugins

To use SWTBot along with your eclipse plugin application you have to add the below plugins to your dependencies. You can download the example from the swtbot download site http://download.eclipse.org/technology/swtbot/docs/eclipsecon2009/examples.zip.

Now you can start using SWTBot. Below you can find a sample SWTBot testcase:

package org.eclipsecon.swtbot.example;
 
import org.eclipse.swtbot.eclipse.finder.SWTWorkbenchBot;
import org.eclipse.swtbot.swt.finder.junit.SWTBotJunit4ClassRunner;
import org.eclipse.swtbot.swt.finder.widgets.SWTBotShell;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
 
@RunWith(SWTBotJunit4ClassRunner.class)
public class MyFirstTest {
 
	private static SWTWorkbenchBot	bot;
 
	@BeforeClass
	public static void beforeClass() throws Exception {
		bot = new SWTWorkbenchBot();
		bot.viewByTitle("Welcome").close();
	}
 
 
	@Test
	public void canCreateANewJavaProject() throws Exception {
		bot.menu("File").menu("New").menu("Project...").click();
 
		SWTBotShell shell = bot.shell("New Project");
		shell.activate();
		bot.tree().expandNode("Java").select("Java Project");
		bot.button("Next >").click();
 
		bot.textWithLabel("Project name:").setText("MyFirstProject");
 
		bot.button("Finish").click();
		// FIXME: assert that the project is actually created, for later
	}
 
 
	@AfterClass
	public static void sleep() {
		bot.sleep(2000);
	}
 
}

[edit] Executing SWTBot Tests for Eclipse Plugins

Now that you've written the great test that you'd always wanted to, lets now see it run. In order to run the test, right click on the test and select Run As > SWTBot Test

Run-as-option.jpg

Select the application that you want to test

Run-as-config-options.jpg

[edit] GEF/GMF-based editor testing

[edit] Intro

SWT has a plugin that allows to manipulate GEF/GMF diagrams, editors and editParts as easily as you can manipulate SWT widgets with SWTBot. Then you can easily create some repeatable user-level UI interations and check their effects on the diagram

[edit] Configuration

The configuration is similar to the one describe before for SWTBot, except that you also have to add org.eclipse.swtbot.eclipse.gef.finder plugin and some other dependencies, such as org.eclipse.ui. In most case, you'll also like to use GEF and/or GMF plugins to make some checks on diagram.

[edit] Getting started with examples

If you like to get started with working examples, you can take a look at the following URL, or check them out in your workspace.

Example GEF project: http://git.eclipse.org/c/swtbot/org.eclipse.swtbot.git/tree/examples/gef/org.eclipse.gef.examples.logic
Example SWTBot for GEF test case: http://git.eclipse.org/c/swtbot/org.eclipse.swtbot.git/tree/examples/gef/org.eclipse.gef.examples.logic.test

[edit] General principles

Everything is almost the same as using SWTBot, except that some classes change in order to give you the ability to manipulate DiagramEditors. The SWTBotTestCase superclass must be replaced by SWTBotGefTestCase. From the inside of your SWTBotTestCase, you can access your SWTGefBot bot field to play with your GEF editor. Then you retrieve a SWTBotGefEditor by using bot.getEditor("label of my editor tab").

Once you have your SWTBotGefEditor, you can perform high level user operations programatically:

[edit] Creation of elements

	// retrieve editor
	SWTBotGefEditor editor = bot.gefEditor("test.logic"); // editor must be already open
	// Simulate creation of element from palette
	editor.activateTool("Circuit");  // "Circuit" is the label of the tool in palette
	editor.mouseDrag(55, 55, 150, 100);
	editor.activateTool("Circuit");
	editor.mouseMoveLeftClick(150, 150);
	editor.activateTool("Connection");
	editor.mouseMoveLeftClick(150, 150);
	editor.mouseMoveLeftClick(55, 55);

[edit] Direct edition of editParts

	SWTBotGefEditPart editPart = editor.getEditPart("edit part label"); // select edit part by label
	editPart.click();
	editor.directEditType("new edit part label");

[edit] Perform a drag'n'drop

	editor.mouseDrag(fromXPosition, fromYPosition, toXPosition, toYPosition)

[edit] Mix it with GEF/GMF to perform checks

TODO: create an example that ensure that creation of a specific element is not possible on mainEditPart

[edit] Recorder and Generator

SWTBot comes with a tool that generates some code based on events performed at runtime. It makes writing tests easier and faster. See SWTBot/Generator.