Reputation: 9
How can I get all nodes of tree in Rational Functional Tester?I have tried it using getTestdata()
but couldn't solve the problem?
Code Snippet:
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)" );
}
But this code is giving me direct children of root node.How can I get all nodes like children of children?
Upvotes: 0
Views: 1013
Reputation: 30
The getTestData("tree")
API should be able to achieve the traversal of nodes of a tree.
Can you please share the code snippet where you are trying to get all the nodes of the tree?
In case you require a snippet, you can use the one below:
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";
Upvotes: 0