Note that SWTBot works on a non-UI thread by default to avoid deadlock with modal dialogs and other user interface actions. If the tests need to interact with specific SWT widgets, it is necessary to invoke a Runnable
via the UI thread.
To make this easier, the SWTBot framework has several helper methods that can provide a facade of the workspace, including the ability to click on buttons and show menus.
UITest
class called createProject
with a @Test
annotation.SWTWorkbenchBot
instance.menu
method of the bot
to navigate the File | Project... menus, and perform a click
.shell
method of the bot
to get the newly opened shell
with a title of New Project
. Activate the shell to ensure that it has focus.tree
method of the bot
to find a tree in the shell, and expandNode
the General
node, and finally select
the Project
.Next >
button with a click
. Note the space between the Next
and the >
symbol.Text
object with the label titled Project name:
and set its text to SWTBot Test Project
.Finish
button.@Test public void createProject() { SWTWorkbenchBot bot = new SWTWorkbenchBot(); bot.menu("File").menu("Project...").click(); SWTBotShell shell = bot.shell("New Project"); shell.activate(); bot.tree().expandNode("General").select("Project"); bot.button("Next >").click(); bot.textWithLabel("Project name:"). setText("SWTBot Test Project"); bot.button("Finish").click(); }
createProject
method does not throw an exception.After creating the workspace bot, the File | Project... menu is selected. Since this opens up a new dialog, a handle needs to be acquired to point to the newly created shell.
To do this, a new SWTBotShell
is created, which is a handle to a displayed shell. The title is used as a key to find a given shell. If one is not currently visible, it polls (every 500 ms by default) until one is found; or the default timeout period (5 s) ends when a WidgetNotFoundException
is thrown.
The activate
method waits until the dialog has focus. To navigate through a dialog, options such as tree
and textWithLabel
methods allow specific elements to be pulled out from the UI, with exceptions being raised if these are not found. If there is only one element of the particular type, then simple accessors such as tree
may be sufficient, but if not, there are xxxWithLabel
and xxxWithId
accessors that can find a specific element in a particular section.
To set an ID on an SWT object such that it can be found with the withId
, call widget.setData(SWTBotPreferences.DEFAULT_KEY, "theWidgetId")
.
When objects are accessed, they aren't the underlying SWT widgets directly. Instead, they are wrappers, much like how the SWTWorkspaceBot
is a wrapper for the workspace. Although the code is calling setText
on what looks like a label, in fact the code is running on a non-UI thread. Instead, it posts a runnable to the UI thread with an instruction to set the text; but all of this is done by SWTBot under the covers.
One thing that's immediately obvious from this is that when using labels, the tests are highly specific to the localization of the product. The tests will fail if the application is run in a different language, for example. They are also implicitly tied to the structure of the application; if the UI changes significantly, then it may be necessary to re-write or update the tests.
Automated testing exercises code paths, but it is often necessary to verify that not only has the user interface reacted in the right way, but also the side effects have happened correctly.
In this case, to find out if a project has been created, use the ResourcesPlugin
(from the org.eclipse.core.resources
bundle) to get the workspace, from which the root will provide a means of accessing an IProject
object. Use the exists
method of the project to determine that the project has been successfully created.
Amend the createProject
method to verify that at the start of the method, the project does not exist, and does exist at the end of the method.
Note that the getProject
method of the IWorkspaceRoot
will return a non-null value regardless of whether the project exists or not.
3.142.12.240