This example shows how to use Functional Tester'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.script.*;
import com.rational.test.ft.value.*;
import com.rational.test.ft.vp.*;
/**
* Description : Functional Tester Script
* @author administrator
**/
public class GetTreeDataExample
extends GetTreeDataExampleHelper
{
/**
* Script Name : <b>GetTreeDataExample</b>
* Generated : <b>Dec 14, 2002 6:24:48 AM</b>
* Modified : <b>Dec 14, 2002 6:24:48 AM</b>
* Description : Functional Tester Script
* Original Host : WinNT Version 5.0 Build 2195 (Service Pack 3)
*
* @since 2002/12/14
* @author administrator
*/
public
void testMain (Object[] args)
{
//Turn off Log Viewer for this example
setOption(IOptionName.BRING_UP_LOGVIEWER, false);
//Start Classics Java Application
startApp("ClassicsJavaA");
//Wait for tree to appear
tree2Tree().waitForExistence();
//Display available test data types available from tree
System.out.println ("Available Tree Data Types: " +
tree2Tree().getTestDataTypes());
//Declare variables for tree
ITestDataTree cdTree;
ITestDataTreeNodes cdTreeNodes;
ITestDataTreeNode[] cdTreeNode;
//Variables to hold tree data
cdTree = (ITestDataTree)tree2Tree().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
ClassicsJavaFrame(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)tree2Tree().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.
Terms of use | Feedback
(C) Copyright IBM Corporation 2002, 2004. All Rights Reserved.