Notice: This Wiki is now read only and edits are no longer possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.
SWTBot/Snippets
SWTBot |
Website |
Update Sites |
Community |
Mailing List |
Forums/Newsgroups |
IRC |
Contribute |
Open Bugzilla tickets |
Open Gerrit reviews |
Browse Source |
Continuous Integration |
Some snippets for working with SWTBot
Contents
Installing/Updating on the command line
Here is a little bash script to help manage SWTBot's version on the command line. Useful for managing an Eclipse installation used for Continuous Integration. It is by no means perfect and you will need to set the proper version for the equinox launcher and the swtbot repository (ganymede, galileo or helios). Feel free to modify to fit your needs.
ECLIPSE_DIR=/usr/local/eclipse # ganymede, galileo or helios REPOSITORY_VERSION=galileo # Uninstall the feature. $ECLIPSE_DIR/eclipse -nosplash -consoleLog \ -application org.eclipse.equinox.p2.director \ -uninstallIU org.eclipse.swtbot.eclipse.gef.feature.group,org.eclipse.swtbot.eclipse.test.junit4.feature.group \ # (Re)Install the feature. This is how you update in the p2 world. $ECLIPSE_DIR/eclipse -nosplash -consoleLog \ -application org.eclipse.equinox.p2.director \ -repository http://download.eclipse.org/technology/swtbot/$REPOSITORY_VERSION/dev-build/update-site/ \ -installIU org.eclipse.swtbot.eclipse.gef.feature.group,org.eclipse.swtbot.eclipse.test.junit4.feature.group \
Note: apparently the application p2.director doesn't exist in eclipse 3.4. The alternative/equivalent is org.eclipse.equinox.p2.director.app.application, but this is an undocumented feature. Another workaround would be to use an eclipse 3.5 to manage an eclipse 3.4, which sounds like the way to go (it's even documented: http://help.eclipse.org/galileo/index.jsp?topic=/org.eclipse.platform.doc.isv/guide/p2_director.html). All you need to do is add -destination eclipse/3.4/path -profile PROFILE when installing, where PROFILE is defined by the property eclipse.p2.profile in the config.ini (configuration/config.ini) of the destination.
Failing from exception in another thread
Most of the time, when you fiddle with the UI, things under the hood might crash and throw exceptions. These exceptions are then catched by Eclipse and logged somewhere so the end-user/developer can have a clue of what went wrong. But when you write UI tests, these exception are not readily available to you and things might falsely succeed or fail with a weird error. So, to have a better fail message, I present you with this little snippet!
public class Logger { private static final ILogListener LISTENER = new ILogListener() { private final List<IStatus> mStatus = new ArrayList<IStatus>(); public void logging(IStatus status, String plugin) { mStatus.add(status); } } @BeforeClass public static void registerLog() { getLog().addLogListener(LISTENER); } @AfterClass public static void unregisterLog() { getLog().removeLogListener(LISTENER); } public static ILog getLog() { return Platform.getLog(Platform.getBundle("org.eclipse.swtbot")); } // A small test case to proof that it is working @Test(expected = NullPointerException.class) public void testLogging() throws Throwable { // This should be done in a @Before or setUp method. To make sure that // errors that happened in the other tests don't pollute this test. clearExceptions(); // Just to test getLog().log(new Status(IStatus.ERROR, "org.eclipse.swtbot", "fake error", new NullPointerException("Fake NPE"))); // This should be done in a @After or tearDown method. checkExceptions(); } public static void clearExceptions() { LISTENER.mStatus.clear(); } public static void checkExceptions() throws Throwable { for (IStatus status : LISTENER.mStatus) { if (status.getException() != null) { // Since it's a list, the first exception that gets logged will be thrown. throw status.getException(); } } } }
Switch perspective
This allows you to switch perspectives in eclipse from within the Window>Open Perspective>Other... menu.
// Change the perspective via the Open Perspective dialog bot.menu("Window").menu("Open Perspective").menu("Other...").click(); SWTBotShell openPerspectiveShell = bot.shell("Open Perspective"); openPerspectiveShell.activate(); // select the dialog bot.table().select("Debug"); bot.button("OK").click();
Tree Expansion
Expansion of items in a SWTBotTree may be tricky, especially if the tree is populated lazily. If a tree item with no children (yet) is expanded, any children added after the expansion will not be seen by SWTBot. You need to collapse and re-expand the item in order to see the new items. A robust solution is to create a wait class that does this automatically. The following snippet shows how to implement such a class:
class WaitForItems extends DefaultCondition { private SWTBotTreeItem item; public WaitForItems(SWTBotTreeItem theItem) { item = theItem; } @Override public boolean test() throws Exception { if (item.select().isExpanded()) item.collapse(); item.expand(); return item.getItems().length > 0; } }