Time for action – interrogating views

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.

  1. In the testTimeZoneView method, get the Widget from the returned SWTBotView.
  2. Create a Matcher using the WidgetMatcherFactory to find widgetsOfType(CTabItem.class).
  3. Use the bot.widgets method to search for a list of CTabItem instances in the view's widget.
  4. Ensure that the number of elements returned is equal to the number of time zone regions.
  5. The code looks like:
    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());
  6. Run the tests and ensure that they pass.

What just happened?

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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.190.253.222