Iterating through items in a tree control using the getTestData method
This topic provides an example of using HCL DevOps Test
UI (Test UI)'s
getTestData
method to programmatically access the values on the
branches of a tree control.
The following example tests against the Classics Java™ application:
import resources.GetTreeDataExampleHelper;
import com.rational.test.ft.*;
import com.rational.test.ft.object.interfaces.*;
import com.rational.test.ft.object.interfaces.SAP.*;
import com.rational.test.ft.object.interfaces.siebel.*;
import com.rational.test.ft.script.*;
import com.rational.test.ft.value.*;
import com.rational.test.ft.vp.*;
/**
* Description : Functional Test Script
* @author Administrator
*/
public class GetTreeDataExample extends GetTreeDataExampleHelper
{
/**
* Script Name : GetTreeDataExample
* Generated : Jul 14, 2006 4:46:31 PM
* Description : Functional Test Script
* Original Host : WinNT Version 5.1 Build 2600 (S)
*
* @since 2006/07/14
* @author Administrator
*/
public void testMain(Object[] args)
{
//Start Classics Java Application
startApp("ClassicsJavaA");
// Frame: ClassicsCD
tree2().waitForExistence();
//Display available test data types available from tree
System.out.println ("Available Tree Data Types: " + tree2().getTestDataTypes());
//Declare variables for tree
ITestDataTree cdTree;
ITestDataTreeNodes cdTreeNodes;
ITestDataTreeNode[] cdTreeNode;
//Variables to hold tree data
cdTree = (ITestDataTree)tree2().getTestData("tree");
cdTreeNodes = cdTree.getTreeNodes();
cdTreeNode = cdTreeNodes.getRootNodes();
//Print out total number of nodes
System.out.println ("Tree Total Node Count: " + cdTreeNodes.getNodeCount());
System.out.println ("Tree Root Node Count : " + cdTreeNodes.getRootNodeCount());
//Iterate through tree branches; this is a recursive method.
for (int i = 0;i<cdTreeNode.length;++i)
showTree(cdTreeNode[i], 0);
//Shut down Classics Java Application
classicsJava(ANY,MAY_EXIT).close();
}
void showTree(ITestDataTreeNode node, int indent)
{
//Recursive method to print out tree nodes with proper indenting.
//Determine number of tabs to use - to properly indent tree
int tabCount = ( indent < tabs.length() ? indent :
tabs.length() );
//Print out node name + number of children
System.out.println(tabs.substring(0, tabCount) + node.getNode() + " (" + node.getChildCount() + "children)" );
//Determine if node has children; recursively call this same
//method to print out child nodes.
ITestDataTreeNode[] children = node.getChildren();
int childCount = ( children != null ? children.length : 0 );
for ( int i = 0; i < childCount; ++i )
showTree(children[i], indent+1);
}
//String of tabs used to indent tree view
final String tabs = "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t";
}
On the first screen of this application is a Java™ Swing JTree
component,
which lists five composers. The next level down lists CDs available for the
selected composer. The code in this sample extracts the values from all of
the branches of the tree and displays them in the console window.
The first step to extracting the data is to use the getTestData
method
to extract the data from the control. This is done with the following syntax:
ITestDataTree cdTree;
cdTree = (ITestDataTree)tree2().getTestData("tree");
The next step is to create an array that contains all of the nodes on the tree. This is done as follows:
ITestDataTreeNodes cdTreeNodes;
ITestDataTreeNode[] cdTreeNode;
cdTreeNodes = cdTree.getTreeNodes();//Encapsulates the root
nodes.
cdTreeNode = cdTreeNodes.getRootNodes();;//Extracts actual
root nodes.
Note that this is a two-step process. First, you must use the getTreeNodes
method
to return a TreeNodes object. Then you can call the getRootNodes
method
to extract an array of the root nodes for the tree.
With the tree nodes in hand, you can use recursion to walk though each
node to determine its value and the number of direct children it contains.
This is done in the recursive method showTree
. A recursive
method is a method that calls itself, and is an efficient way to walk through
a tree structure. To extract the value of the node, the getNode
method
is used. To extract the number of children contained by the node, the getChildCount
method
is used. In the example, this is done with the following code:
System.out.println(tabs.substring(0, tabCount) + node.getNode()+" (" + node.getChildCount() + " children)");
Note the additional coding provided in the custom showTree
method
is to enable a formatted printing using tabs to indicate the indentation of
the tree.