0

如何在 Rational Functional Tester 中获取树的所有节点?我已经尝试使用getTestdata()但无法解决问题?

代码片段:

  ITestDataTree iTreeData =(ITestDataTree)businessObj.smi_LocTreePricingRules().getTestData("tree");
               ITestDataTreeNodes iNodes = iTreeData.getTreeNodes();
               System.out.println("node count:"+iNodes.getNodeCount());

               ITestDataTreeNode[] rootNodes = iNodes.getRootNodes();
             // System.out.println(iNodes.getNodeCount());
             //  ITestDataTreeNodes nodes= tree.getTreeNodes();

              System.out.println(rootNodes.length);
               for(int i = 0; i < rootNodes.length; i++) {
                genericObj.showTree(rootNodes[i]);   //This showTree method is present in another GenericLib class.                                                      public void showTree(ITestDataTreeNode node)
{

    //Recursive method to print out tree nodes with proper indenting.

    //Determine number of tabs to use - to properly indent tree


    //Print out node name + number of children
    //System.out.println(  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]);
    String synb = node.getNode().toString();

    System.out.println(  node.getNode() + " (" + node.getChildCount() + "children)" );


}

但是这段代码给了我根节点的直接子节点。我怎样才能得到像子节点一样的所有节点?

4

1 回答 1

0

getTestData("tree")API应该能够实现树的节点的遍历。您能否分享您尝试获取树的所有节点的代码片段?

如果您需要片段,可以使用以下片段:

public printTreeNodes(){
//Declare variables for tree
        ITestDataTree cdTree;
        ITestDataTreeNodes cdTreeNodes;
        ITestDataTreeNode[] cdTreeNode;

        //Variables to hold tree data
                //tree_mytree() is the tree object you want to traverse
        cdTree = (ITestDataTree)tree_mytree().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);
}

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";
于 2013-10-09T18:48:05.520 回答