以下示例针对 Classics Java™ 应用程序进行测试:
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";
}
该应用程序的第一个屏幕上是一个 Java Swing JTree 组件,该组件列出了五位作曲家。下面一级列出了选定作曲家的可用 CD。本样本中的代码从树的所有分支中抽取值,并在控制台窗口中进行显示。
抽取数据的第一步是使用 getTestData 方法来从控件中抽取数据。此操作使用以下语法来完成:
ITestDataTree cdTree;
cdTree = (ITestDataTree)tree2().getTestData("tree");
下一步是创建包含树上所有节点的数组。此操作通过如下所示实现:
ITestDataTreeNodes cdTreeNodes; ITestDataTreeNode[] cdTreeNode; cdTreeNodes = cdTree.getTreeNodes();//Encapsulates the root nodes. cdTreeNode = cdTreeNodes.getRootNodes();;//Extracts actual root nodes.
请注意,该过程需要两个步骤。首先,必须使用 getTreeNodes 方法来返回 TreeNodes 对象。然后,可以调用 getRootNodes 方法来抽取树的根节点的数组。
拥有树节点后,可以使用递归来浏览各节点,以确定其值及其包含的直接子代数。 此操作通过递归方法 showTree 来完成。递归方法是调用其自身的方法,并且是浏览树结构的有效方式。为了抽取节点的值,会使用 getNode 方法。为了抽取节点所包含的子代数,会使用 getChildCount 方法。在此示例中,会使用以下代码来执行此操作:
System.out.println(tabs.substring(0, tabCount) + node.getNode()+" (" + node.getChildCount() + " children)");
请注意,定制 showTree 方法中所提供的其他编码是通过用于指示树缩进的制表符来支持格式化打印。