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 labels, the bot provides standard methods. 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 that is based on widgetsOfType(CTabItem.class).
  3. Use bot.widgets() to search for a list of CTabItem instances in the view's widget.
  4. Ensure that 18 elements are returned. Check the running application to verify how many tabs there are in the time zone view; there may be more or less, depending on how many timezones your computer has.
  5. The code looks like this:
    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);
    assertEquals(18,ctabs.size());
  6. Run the tests and ensure that they are successful.

What just happened?

It is possible to write code to walk the user interface tree directly. However, because the widgets have to be interrogated on the UI thread, 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 that have a certain class type; similarly many other matchers can be instantiated with the withXxx() calls, such as withLabel() and withId(). Matchers can be combined with the allOf() and anyOf() methods, by providing AND/OR logic respectively.

The widgets() call walks through the tree recursively to find all widgets that match a 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 in this case is 18.

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

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