Site Loader
Auckland, New Zealand
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
  • Get all the controls of our Application Under Test (AUT) and also we will get their properties.
  • We will get the number of controls within application of same type (WinButton, WinText etc)
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

Post Author: Karthik kk

13 Replies to “Getting all the Controls properties from Application in Coded UI Test”

  1. Hi,

    I am getting an error at:
    var controls = controlCol.Select(x => x.Name);

    I seems like controlCol.Select() method doesn’t exists? I’m using VS 2013 Premium.
    Thanks
    -Asif

    1. Hi Asif,

      Thanks for reading the post !!

      The Select() is the extension method which is available with LINQ.

      Try to add the namespace for LINQ and this should work.

      Thanks,
      Karthik KK

    2. Hi Asif,

      Here is the code, to get all the menu item controls

       UITestControl menu = new WinMenuItem(calcWindow);
      
                  UITestControlCollection controlCols = menu.FindMatchingControls();
      
                  var controls = controlCols.Select(x => x.Name);
      
                  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());
                  }
      

      And its working fine for me !!!

      I have added following LINQ reference

       using System.Linq; 

      Thanks,
      Karthik KK

  2. Hi Karthik,

    Thanks for your very useful information.

    I just want to leave a note for those who have DevExpress Components in their applicatons.

    It works well with MSAA control. However, FindMatchingControls() method is not able to get controls for DevExpress components due to the reasons that there is no DXTestControlCollection, as far as I inferred.

    The only solution to be able to find the controls is defining the whole control hierarchy.

    The issue had my hands full for days. Just wanted to inform people who will face with the same problem 🙂

  3. Is there a way to access more than MSAA / UAI properties of an object? In Test Complete we had access to all public class objects, methods, and variables of an object. We moved away from test complete because of performance and stability issues, but want to maintain that level of testing.

  4. Need a help with instance property that is used for indexing child controls.
    Could you please provide guidance.

    Please let me know if you have any video regarding this as well.

    Thanks,
    Sagar

  5. Great article, but I am getting an error on FindMAtchingControls
    Error message:”+ $exception {“No search information has been specified. To search for a control, you must specify one or more search properties or filter properties.\r\nParameter name: SearchProperties”} System.ArgumentException

    Any help would be appreciated

    Full code:
    public UITestControl findControl(WpfControl parentControl, string controlProperty)
    {
    UITestControl cntrlToFind = new UITestControl(parentControl);
    UITestControlCollection controlCol = cntrlToFind.FindMatchingControls();
    var controls = controlCol.Select(x => x.Name);
    Console.WriteLine(“The number of controls in my AUT is : ” + controls.Count());
    foreach (var control in controls)
    {
    Console.WriteLine(control.ToString());
    }

    cntrlToFind.SearchProperties[UITestControl.PropertyNames.Name] = controlProperty;

    return cntrlToFind;
    }

    1. Hi ! Since I’m getting a similar kind of error, it would be great help if you could let me know how did you rectify it? Also, is there any alternative available for the same?

  6. Hi Karthik,

    Thanks for sharing this information.

    I am facing issue in finding controls (it is third party controls – DotNetBars).

    Even i am unable to spy these controls . it is throwing Object instance set to null error.

    Please suggest me how to automate this control in codedui

    1. Which means the control is either not supported by coded ui or your visual studio has to be reinstalled if it happens to be problem with visual studio installation (may be, not sure)

      Thanks,
      Karthik KK

  7. Hi Karthik,
    I would like to know how to edit/add properties for a control that doesn’t have a Name/DisplayText, or any other unique property for a desktop application ?

    Thanks,
    Pro

Leave a Reply

Your email address will not be published. Required fields are marked *