GUI automation always depends on the type of used controls and technologies within applications. An increasing use of 3rd party controls in applications also makes GUI automation more and more addicted to identification information provided by the 3rd party control. In most cases providers of 3rd party controls support enough information to get a control’s elements identified and automated. Nevertheless, there are sometimes more complex ways required to simulate user actions. That type of test automation code is not that “readable” as we would like to have.

Ranorex V1.4 supports accessing properties and methods of .NET derived controls. Following “Design for Testability” sample explains the way of making testing more comfortable. By using the 3rd party UltraTree control from Infragistics the example describes how to access user defined methods for test automation.

Please notice that this blog post targets Ranorex 1.X and might be obsolete if you use Ranorex 2.x or newer!”

Invoke overview

In fact there should only be 3 steps required to expand or select a specific node within the tree view control.

  1. Searching for the application
  2. Finding tree view control within form
  3. Invoking method with simple string path “Root/Child_02/”
Form form = Application.FindFormTitle("Design for Testability",
				           SearchMatchMode.MatchFromStart);
Control tree = form.FindControlName("ultraTree1");
tree.InvokeMethod("ExpandNode", null, "Root/Child_02/");

To invoke a method called “ExpandNode” we only have to provide this method within a class derived from the Infragistics UltraTreeView control:

/// Derived class from Infragistics UltraTree control
public class MyTreeView : Infragistics.Win.UltraWinTree.UltraTree
{
    public MyTreeView()
    {}
    /// This method steps through each sub node of
    /// the tree view control.
    public void ExpandNode(UltraTreeNode node, string path)
    {
        string[] nodeList = path.Split('/');
        UltraTreeNode currentNode = null;
        TreeNodesCollection treeNodes = null;
        // Start from controls node list if null
        if (node == null)
            treeNodes = this.Nodes;
        else
            treeNodes = node.Nodes;
        foreach (UltraTreeNode n in treeNodes)
        {
            if (n.Text == nodeList[0])
            {
                currentNode = n;
                // Expand node
                n.Toggle();
                // Select node
                n.Selected = true;
                break;
            }
        }
        if (currentNode == null)
            return;
        // start expanding for sub nodes
        ExpandNode(currentNode,path.Remove(0,currentNode.Text.Length+1));
    }
}

Invoking methods or properties can be a powerful mechanism to simplify automation of 3rd party controls, if developers consider testability during their software design and implementation process.

You might also like these articles