The first step is to install SWTBot from the Eclipse update site. These examples were tested with http://download.eclipse.org/technology/swtbot/releases/latest/ version 2.3.0, but check out the book's errata for up-to-date information.
com.packtpub.e4.junit.plugin
project:org.eclipse.swtbot.junit4_x
org.eclipse.swtbot.forms.finder
org.eclipse.swtbot.eclipse.finder
org.eclipse.ui
UITest
in the com.packtpub.e4.junit.plugin
package.@RunWith(SWTBotJunit4ClassRunner.class)
.testUI
and with an annotation @Test
.testUI
method, create an instance of SWTWorkbenchBot
.shells
and assert that the one that is visible has a title containing Eclipse. The code looks like:package com.packtpub.e4.junit.plugin; import static org.junit.Assert.assertTrue; 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.Test; import org.junit.runner.RunWith; @RunWith(SWTBotJunit4ClassRunner.class) public class UITest { @Test public void testUI() { SWTWorkbenchBot bot = new SWTWorkbenchBot(); SWTBotShell[] shells = bot.shells(); boolean found = false; for (int i = 0; i < shells.length && !found; i++) { if (shells[i].isVisible()) { if (shells[i].getText().contains("Eclipse")) { found = true; } } } assertTrue(found); } }
SWTBot is a UI testing mechanism that allows the state of the user interface to be driven programmatically. In this test, a new SWTWorkbenchBot
was created to interact with the Eclipse workbench (for pure SWT applications, the SWTBot
class exists).
The bot iterates through the available shells once the workspace has been opened. Although more than one shell is returned in the list, not all of them will be visible. The shell's title can be obtained through the getText
method, which returns Java – Eclipse SDK
if the Eclipse SDK package opens on the Java perspective by default—but this value may differ depending on what perspective and which Eclipse package is being used.
The application run is similar to an Eclipse product launch; combinations of plug-ins, start-up properties, and product or application choices can be made via the Run or Debug configurations menu. As with ordinary JUnit tests, the launch can be invoked in Debug mode and the breakpoints set.
3.142.35.75