Having been able to acquire a reference to the view, the next step is to deal with specific user interface components. For standard controls such as Button
and Text
, the bot
provides standard methods to acquire. To get hold of other components, the widget hierarchy will have to be interrogated directly.
testTimeZoneView
method, get the Widget
from the returned SWTBotView
.Matcher
using the WidgetMatcherFactory
to find widgetsOfType(CTabItem.class)
.bot.widgets
method to search for a list of CTabItem
instances in the view's widget.SWTBotView timeZoneView = bot.viewByTitle("Time Zone View"); assertNotNull(timeZoneView); Widget widget = timeZoneView.getWidget(); org.hamcrest.Matcher<CTabItem> matcher = WidgetMatcherFactory.widgetOfType(CTabItem.class); final java.util.List<? extends CTabItem> ctabs = bot.widgets(matcher, widget); Set<String> regions = ZoneId.getAvailableZoneIds() // get zones .stream().filter(s -> s.contains("/")) // with / in them .map(s -> s.split("/")[0]) // get first part .collect(Collectors.toSet()); // and convert to a set assertEquals(regions.size(), ctabs.size());
Although it is possible to write code to walk the user interface tree directly, the widgets have to be interrogated on the UI thread and so care has to be taken to find the children.
SWTBot provides a generic Matcher
mechanism, which is a predicate that can return true
if a certain condition occurs. The Matcher
provided by widgetOfType
matches items which have a certain class type; similarly many other matchers can be instantiated with the withXxx
calls, such as withLabel
and withId
. The Matcher
instances can be combined with allOf
and anyOf
methods providing and/or logic respectively.
The widgets
method walks through the tree recursively to find all widgets that match the particular specification. A single-argument version finds all elements from the active shell; the two-argument version allows a specific parent to be searched, which in this case is the TimeZoneView
itself.
Finally, the size of the list is compared with the number of time zone groups, which is dynamically calculated from the running platform.
18.223.149.223