FeaturesPluginsDocs & SupportCommunityPartners

Component Name in tests

Author: Jiri Skrivanek
Last update: April 19, 2005



For test case developers is vital to be able to uniquely distinguish components in tested application. It can be done several ways because components have a lot of characteristics. The goal is to select that one which will uniquely identify a component, will be easy to find both for test developer and test library. It seemed that mentioned criteria are fulfilled by text visible in component (e.g. title of a window or dialog, caption of a button and so on). But later it comes to light that such a string might be localized on non-english locales. That means test developer has to modify tests for every platform on which he wants to execute tests. Of course, it is time consuming. Our chance is to use strings from resource bundles. It was successful approach. Support for that exists in core of NetBeans IDE and handy utilities were developed to search in bundles and use strings from bundles in runtime. You can look at jellytools module documentation.
Usage of component's name seems even easier and error-proner that usage of resource bundles. It has only drawback that not every component has a name asigned. But it is desirable for testability to have all components covered by names. The following paragraphs should give you an idea how to use component's names in tests.

How to use component name in tests

In Jemmy there exists org.netbeans.jemmy.util.NameComponentChooser which is intended to search components by its name. Its usage is straightforward because constructors of jemmy operators accept any ComponentChooser implementation as parameter. To find a JButton named btSelect in a container represented by containerOperator you can write the following:

        new JButtonOperator(containerOperator, new NameComponentChooser("btSelect"));

Similarly it is applicable for other operators.

How to find out component name

Look at sources

You can look at source code of UI on which you want to execute test. Component name is set by method java.awt.Component.setName(String). You can search java code for occurences of setName() or you can use NetBeans' form editor to inspect name property.

Use Component Name Lookup from Jemmy support module

You can use Component Name Lookup from Jemmy Support module. Just install the latest version of module from NetBeans Update Center Alpha and open Component Name Lookup panel from toolbar or main menu Tools|Component Name Lookup. When you move mouse over a component, you will see its name and class name. It works for whole IDE and applications run internally (in the same VM).

Use extended Accessibility tester

You can use extended Accessibility Tester to test containers at once. General documentation can be found at a11y.netbeans.org. Here is imple description how to use it:
  • install Accessibility module from NetBeans Update Center
  • run IDE with switches -J-Da11ytest.IDE=true and -J-Da11ytest.name=true
  • push icon in toolbar to invoke UI Accessibility Tester.
  • check Test component name check box
  • uncheck accessibility tests if you don't want to test it
  • push button Run test
  • open window that you want to test and press CTRL+F11
  • check results after test is finished
    •  components without name are printed out to standard output (by default)
    • AWT tree shows hierrarchy of components and when you select component its name is shown below the tree

How to check absence of component names

To find out whether all components which I want to test have component name set, you can use all methods described in paragraph How to find out component name. In addition you can use command-line utility which searches forms created in NetBeans. It checks *.form files in given directory and its sub directories. It assumes majority of NetBeans' forms has assocciated .form file. Usage is simple, e.g.:

        java -jar checkForms.jar D:\\cvs\\nb_all\\ant

As the result it prints list of components which don't have component name set. Executable jar file can be dowloaded here.

How to complete names for all components

Developers of source code should keep in mind that all UI components have to have component name in order to be better testable. If you find out that a component which you want to test is without name, you can request adding of missing names. You can file an issue to issuezilla with keyword T9Y to appropriate module. Or you can fix source code yourself and attach patch to issue. Or even you can check updates in CVS repository but only with previous approval of module owner or responsible developer.

Companion
Projects:
MySQL Database Server   Open JDK: an Open SourceJDK   GlassFish Community: an Open Source Application Server    Mobile & Embedded Community    Open Solaris   java.net - The Source for Java Technology Collaboration   Virtual Box - full virtualizer  Open ESB - The Open Enterprise Service Bus Powered by