Home > Articles > Programming

This chapter is from the book

Using getTestData() to Extract Data from a Tree

From a test data type perspective, trees are simple. There are only two test data types: tree and selected.

Table 3.7 Tree Object Data Types

Data Type

Return Type




Retrieves the entire tree hierarchy.



Retrieves the selected tree nodes.

Tree Talk

There’s a special vocabulary used with trees with which you might not be familiar. A tree consists of elements referred to as nodes. Each node can have a parent, siblings, and children. A node that has no parent is called a root node. A tree can have one root node (the Classics music tree) or multiple root nodes (the tree in the Rational Functional Tester Preferences dialog box). A node that has no children is called a leaf or terminal node.

In the Classics music tree, Composers is the root node. Composers has no siblings. Its children are Schubert, Haydn, Bach, Beethoven, and Mozart. Mozart has three children. Symphony No. 34 has no children and is therefore a leaf node.

Before continuing, you need to clarify what is returned with respect to selected tree nodes. If you click Bach’s Violin Concertos in Classics, it is the selected node. You might think that in this scenario if you get selected data from the tree, just Violin Concertos will be returned. This is not the case. To see what getTestData() returns, insert a Data verification point on the tree with Bach’s Violin Concertos selected and select Selected tree hierarchy. You see something similar to what is displayed in Figure 3.22.

Notice that Rational Functional Tester captures the selected element along with its ancestor nodes all the way up to the root node. If multiple nodes are selected (hold down the Ctrl key and select Symphony No. 9 and Mozart’s Symphony in C, No. 41: Jupiter), the selected tree hierarchy would look like Figure 3.23.

Table 3.8 lists the interfaces for accessing tree data and the commonly used methods in each interface.

Table 3.8 Interfaces and Their Methods Used for Accessing Trees




ITestDataTreeNodes getTreeNodes()

ITestDataTree Nodes

int getNodeCount() int getRootNodeCount() ITestDataTree
Node[] getRootNodes()

ITestDataTree Node

int getChildCount() ItestDataTreeNode[] getChildren()
ItestDataTreeNode getParent() Object getNode()

The following steps can be used as a generic template for acquiring the root nodes of a tree control.

  1. Call the tree control’s getTestData() method. The data will be returned as an ITestDataTree interface.
  2. On the ITestDataTree reference, call getTreeNodes(). This returns the data as an ITestDataTreeNodes interface.

    Note that if you want to get to the selected leaf nodes in the tree, you need to start at the top of the tree and traverse down to them (such as using some sort of loop). To get to the top of a tree, call getRootNodes(), which returns an array of individual ITestDataTreeNodes. It returns an array because depending on the tree, there can be more than one root node.

    Also note that after you have the data contained in an ITestDataTreeNodes interface, you can find out how many nodes are in the data returned by calling getNodeCount(). It’s important to note that getNodeCount() doesn’t necessarily return the number of nodes in the test object; it returns the number of nodes in the data returned by the initial call to getTestData(). If you pass getTestData() an argument of tree, it returns the total number of nodes in the tree (for example, the Composer tree in Classics returns 20 nodes). If you pass an argument of selected, it returns the number of nodes in the selected tree hierarchy. If Bach Violin Concertos is selected, it returns three (because the nodes returned are Composers > Bach > Violin Concertos).

  3. As listed in Table 3.8, there are three key methods you can invoke on an ITestDataTreeNode. getNode() returns the node data as an Object. Thus, if you call getNode() on the first (and only) root node and cast that to a string, you can print it out (or more likely store it in a variable for further processing).

Listing 3.11 provides two examples—one Java and one VB.NET—that exemplify the previous template steps.

Listing 3.11. Printing a tree’s root nodes

public void printRootNodes( TestObject tree ) {
   String selectedNode = null;

   ITestDataTree iTreeData =
   ITestDataTreeNodes iNodes = iTreeData.getTreeNodes();
   ITestDataTreeNode[] rootNodes = iNodes.getRootNodes();
   for(int i = 0; i < rootNodes.length; i++) {
      String nodeData = rootNodes[i].getNode().toString();
      System.out.println( nodeData );
Public Sub printRootNodes(ByVal tree As TestObject)
   Dim selectedNode As String = Nothing
   Dim iTreeData As ITestDataTree = tree.GetTestData("tree")
   Dim iNodes As ITestDataTreeNodes = iTreeData.GetTreeNodes()

   Dim rootNodes As ITestDataTreeNode() = iNodes.GetRootNodes()
   For i As Integer = 0 To rootNodes.length - 1
      Dim nodeData As String = rootNodes(i).GetNode.ToString
      Console.WriteLine( nodeData )
End Sub

The next code sample in Listing 3.12 builds off the prior steps. In particular, after you acquire the root node as an ITestDataTreeNode, you can start traversing it. To descend one level, call getChildren(). This returns the ITestDataTreeNode’s children (in the returned data, not the test object) as an array of ITestDataTreeNodes. Note that the data type of the root nodes is the same as any other node in a tree, namely ITestDataTreeNode. After you have the array of child ITestDataTreeNodes, you can acquire their data, using the getNode() method and casting it to a string—similar to what was accomplished in Listing 3.11.

Listing 3.12. Get the text of the selected node in a tree

public String getSelectedTreeNode( TestObject tree ) {
   String selectedNode = null;

   ITestDataTree iTreeData =
   ITestDataTreeNodes iNodes = iTreeData.getTreeNodes();
   int nodeCount = iNodes.getNodeCount();
   System.out.println("node count = " + nodeCount);
   if( nodeCount != 0) {
      ITestDataTreeNode[] node = iNodes.getRootNodes();
      for(int i = 0; i < nodeCount - 1; i++) {
         ITestDataTreeNode[] children = node[0].getChildren();
         node = children;
      selectedNode = node[0].getNode().toString();
   return selectedNode;
Public Function getSelectedTreeNode(ByVal tree As TestObject) As String
   Dim selectedNode As String = Nothing
   Dim iTreeData As ITestDataTree = tree.GetTestData("selected")
   Dim iNodes As ITestDataTreeNodes = iTreeData.GetTreeNodes()
   Dim nodeCount As Integer = iNodes.GetNodeCount()

   Console.WriteLine("Nodecount = " + nodeCount.ToString)
   If (nodeCount <> 0) Then
      Dim node As ITestDataTreeNode() = iNodes.GetRootNodes()
      For i As Integer = 1 To nodeCount - 1
         Dim children As ITestDataTreeNode() = node(0).GetChildren()
         node = children
      selectedNode = node(0).GetNode().ToString()
   End If

   Return selectedNode
End Function
  • + Share This
  • 🔖 Save To Your Account