Iterating through items in a tree control using the GetTestData method

This topic provides an example of using Rational® 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:

#Region " Script Header "
' Functional Test Script
' author Administrator
Imports Microsoft.VisualBasic
Imports Rational.Test.Ft
Imports Rational.Test.Ft.Object.Interfaces
Imports Rational.Test.Ft.Object.Interfaces.SAP
Imports Rational.Test.Ft.Object.Interfaces.Siebel
Imports Rational.Test.Ft.Script
Imports Rational.Test.Ft.Value
Imports Rational.Test.Ft.Vp
#End Region

Public Class GetTreeDataExample 
    Inherits GetTreeDataExampleHelper

    'Script Name   : GetTreeDataExample
    'Generated     : Jul 17, 2006 3:25:22 PM
    'Description   : Functional Test Script
    'Original Host : Windows XP x86 5.1 build 2600 Service Pack 2 

    'since  2006/07/17
    'author Administrator

    Public Function TestMain(ByVal args() As Object)
        ' Turn off Log Viewer for this example
        SetOption(IOptionName.BRING_UP_LOGVIEWER, False)

        'Start Classics Java Application
        StartApp("ClassicsJavaA")

        ' Wait for tree to appear
        Tree2().WaitForExistence()

        ' Display available test data types available from tree
        System.Console.WriteLine(String.Format("Available Tree Data Types: {0}", Tree2().GetTestDataTypes()))

        ' Declare variables for tree
        Dim CdTree As ITestDataTree
        Dim CdTreeNodes As ITestDataTreeNodes
        Dim CdTreeNode() As ITestDataTreeNode

        ' Variables to hold tree data
        CdTree = Tree2().GetTestData("tree")
        CdTreeNodes = CdTree.GetTreeNodes()
        CdTreeNode = CdTreeNodes.GetRootNodes()


        ' Print out total number of nodes
        System.Console.WriteLine("Tree Total Node Count: " + CdTreeNodes.GetNodeCount())
        System.Console.WriteLine("Tree Root Node Count : " + CdTreeNodes.GetRootNodeCount())

        ' Iterate through tree branches; this is a recursive method.

        Dim I As Integer

        For I = 0 To CdTreeNode.Length - 1
            ShowTree(CdTreeNode(I), 0)
        Next I

        ' Shut down Classics Java Application
        ClassicsJava(ANY, MAY_EXIT).Close()
    End Function

    Public Sub ShowTree(ByVal Node As ITestDataTreeNode, ByVal _
        Indent As Integer)
        ' Recursive method to print out tree nodes with proper
        ' indenting.

        ' Determine number of tabs to use - to properly indent tree
        Dim TabCount As Integer
        IIf(Indent < Tabs.Length(), TabCount = Indent, TabCount = Tabs.Length())

        ' Print out node name + number of children
        System.Console.WriteLine(Tabs.Substring(0, TabCount) + Node.GetNode() + " (" + Node.GetChildCount() + " Children)")

        ' Determine if node has children; recursively call this same
        ' method to print out child nodes.
        Dim Children() As ITestDataTreeNode = Node.GetChildren()
        Dim ChildCount As Integer

        IIf(ChildCount <> 0, ChildCount = Children.Length, ChildCount = 0)

        Dim I As Integer

        For I = 0 To ChildCount - 1
            ShowTree(Children(I), Indent + 1)
        Next I
    End Sub

    ' String of tabs used to indent tree view
    Const Tabs As String = _
    "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t"

End Class


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:


     Dim CdTree As ITestDataTree
     CdTree = 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:


     Dim CdTreeNodes As ITestDataTreeNodes
     Dim CdTreeNode As ITestDataTreeNode()
 
     CdTreeNodes = CdTree.GetTreeNodes()
     CdTreeNode = CdTreeNodes.GetRootNodes()

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.Console.WriteLine(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.