ExecuteAutomation

Getting all the Controls properties from Application in Coded UI Test

In the last post, we discussed working with UITestControl class to generically identify controls and working with them for a Windows application. In this post, we will discuss a little more options which UITestControl class provide us while working with multiple controls in our application. In this post, we will try to This will come handy while working with larger framework design and complex code in Coded UI Testing scenario. We will use LINQ to grab all the value from collection and get a specific property of the control. You can learn more about LINQ from LINQ Series available right here Here is the code snippet for finding/getting all the control from application. We are trying to grab all the button control from the calculator as shown below

Figure 1: Calculator Application

    //Collection of Matching Controls
    UITestControlCollection controlCol = btn8.FindMatchingControls();
    //Select the Name property from Collection via LINQ
    var controls = controlCol.Select(x => x.Name);
    //Get the count
    Console.WriteLine("The number of controls in my AUT is : " + controls.Count());

    //Iterate through all the values of all the controls
    foreach (var control in controls)
    {
        Console.WriteLine(control.ToString());
    }

Here is the complete code
public static void Button8Click()
{

    //Instance for WinWindow
    UITestControl calcWindow = new UITestControl();
    calcWindow.TechnologyName = "MSAA";
    calcWindow.SearchProperties[UITestControl.PropertyNames.Name] = "Calculator";
    calcWindow.SearchProperties[UITestControl.PropertyNames.ClassName] = "CalcFrame";

    //Button
    UITestControl btn8 = new WinButton(calcWindow);

    //Collection of Matching Controls
    UITestControlCollection controlCol = btn8.FindMatchingControls();
    //Select the Name property from Collection via LINQ
    var controls = controlCol.Select(x => x.Name);
    //Get the count
    Console.WriteLine("The number of controls in my AUT is : " + controls.Count());

    //Iterate through all the values of all the controls
    foreach (var control in controls)
    {
        Console.WriteLine(control.ToString());
    }


    btn8.SearchProperties[UITestControl.PropertyNames.Name] = "8";

    Mouse.Click(btn8);

    UITestControl btn9 = new WinButton(calcWindow);
    btn9.SearchProperties[UITestControl.PropertyNames.Name] = "9";
    Mouse.Click(btn9);

    UITestControl btnAdd = new WinButton(calcWindow);
    btnAdd.SearchProperties[UITestControl.PropertyNames.Name] = "Add";
    Mouse.Click(btnAdd);

    UITestControl btn2 = new WinButton(calcWindow);
    btn2.SearchProperties[UITestControl.PropertyNames.Name] = "2";
    Mouse.Click(btn2);

    UITestControl btn3 = new WinButton(calcWindow);
    btn3.SearchProperties[UITestControl.PropertyNames.Name] = "3";
    Mouse.Click(btn3);

    UITestControl btnEquals = new WinButton(calcWindow);
    btnEquals.SearchProperties[UITestControl.PropertyNames.Name] = "Equals";
    Mouse.Click(btnEquals);
}
Here is the Video for the above post Thanks for reading the post and watching the video !!! Please leave your comments and let me know if you like it and wanna improve it. Thanks, Karthik KK