Site Loader
Auckland, New Zealand
There are many situations where we might need to get all the elements available from within a Page, Div, Table etc. The elements can be either list of text box, checkbox, hyperlinks etc. In QTP we can do this very easily by means of ChildCount method to return the list of child within a particular object, similarly in Visual Studio 2010 Coded UI Testing we can do the same via UITestControlcollection collection and FindMatchingControls method. Consider a scenario where you are asked to list all the Hyperlinks within a Div element of a page. Then all you have to do is follow the three simple steps
  1. Identify the Div name (It can be for e.g. HeaderContent)
  2. Pass the Div name instance object to HTML control class (Now this will have all your controls within the particular Div, note this can be anything not just hyperlink alone which we are searching)
  3. Using FindMatchingControl method, search for the control you are interested in searching for, here its hyperlink (HtmlHyperLink class in VSTS) and pass it to UITestControlCollection.
  4. Now you have the handle for getting the controls, you can now iterate through the collection and get all the matching controls value out from it. That’s it !!!
Let’s see the above operation in code
public void SearchhyperLinks()
        {
            //Search for the Div which has the list of hyperlinks we are searching
            HtmlDiv div = new HtmlDiv();
            div.SearchProperties[HtmlDiv.PropertyNames.Name] = "HeaderContent";
            //Pass the instance of Div to HtmlControl class
            HtmlControl controls = new HtmlControl(div);
            controls.SearchProperties.Add(HtmlControl.PropertyNames.ClassName, "HtmlHyperlink");
            UITestControlCollection collection = controls.FindMatchingControls();
            foreach (UITestControl links in collection)
            {
                //cast the item to HtmlHyperlink type
                HtmlHyperlink mylink = (HtmlHyperlink)links;
                //get the innertext from the link, which inturn returns the link value itself
                Console.WriteLine(mylink.InnerText);
            }
        }
Thanqks, Karthik KK

Post Author: Karthik kk

20 Replies to “How to identify all the child elements within a page in Coded UI Test (CUIT)”

  1. I made a test based on your code but it runs too slow. This happens because It finds multiple instances in the “controls.SearchProperties.Add…” line and just keeps running that step over and over trying to find a single instance. It gives up eventually after 2 minutes or so. I don’t how to make it stop when those multiple instances are found in the first place :/

    1. You are correct. The code I wrote is just a basic code of how to work with child elements, but to get complete detail, please checkout the following links
      http://executeautomation.com/blog/understanding-why-and-how-parent-child-relationship-works-in-coded-ui-testing/
      http://executeautomation.com/blog/creating-custom-reusable-methods-for-different-controls-in-coded-ui-testing/

      If you follow the link 2 guidelines, the code will be faster.

      Thanks,
      Karthik KK

      1. Actually, it seems like it’s not that slow as I was thinking. I was using a “Console.Writeline” inside the cycle and simply removing that line made the test way quicker. Do you know what is the best way to print info to the output console?

        Thanks for the useful links anyway and for your quick answer,
        Mauro

        1. Try to change the IE driver with ChromeDriver, it should work fine. Nothing to do with Console.Writeline.

          Thanks,
          Karthik KK

  2. I have a Pane, under that pane there is some childern. it looks like below
    ———- (First name)
    ———-(Sec Name)
    ——–(Third name)
    Here i want to click on “Third name” , may i know how to do a wrapper class for this please?

  3. Hello,
    My problem is with dynamic id which keeps on changing daily, is there some thing like x-path in selenium in CUIT where I can actually point to that control with out using tat dynamic id

    1. You can read here https://blogs.msdn.microsoft.com/visualstudioalm/2011/12/28/sample-xpath-utility-for-coded-ui-test/, I tried it personally before couple of years and it worked !

      Here is the download link, but again, its very old and not in development as well, so you need to see if its compatible with latest version of Visual studio https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Components.PostAttachments/00/10/25/14/61/UITestXPathUtility.zip

      Thanks,
      Karthik KK

  4. Hi,

    There is a search box in home page after searching i got some reports there are buttons download excel and download csv when clicked on those buttons file will get download. How to check whether the file is downloaded or not and each file may take different amount of time ? I dont want to check in the downloaded path since the path may vary for different system. I want to check in the download options in the browser is it possible?

    1. Hi Ramya,

      That’s very good Qs, perhaps you can use below code. This works quite fine for me.But the limitation is it only will check in downloads folder. But you can customize it very easily.

      WinToolBar custom = new WinToolBar(localBrowser);
      custom.SearchProperties[WinToolBar.PropertyNames.Name] = “Notification”;
      custom.SearchProperties[WinToolBar.PropertyNames.ClassName] = “DirectUIHWND”;

      WinSplitButton save = new WinSplitButton(custom);
      save.SearchProperties[WinSplitButton.PropertyNames.Name] = “Save”;

      Mouse.Click(save);

      Playback.PlaybackSettings.WaitForReadyLevel = WaitForReadyLevel.AllThreads;
      return File.Exists(Path.Combine(GetFolderPath(SpecialFolder.UserProfile),”downloads”,filename));

  5. How to select a item from dropdown without using the id / class properties. I have a page it has 11 dropdowns with the same class name and no id only the taginstance is varying but if we are using that it is showing control not found exception what to do?

  6. Does FindMatchingControls() actually scroll to the control if the control is not visible? because i see that happening in my CodedUI test. I do not want that behaviour , is there an alternative to this method ?

  7. Hi Karthik,

    Ho to create instance for ControlType : Pane, as we don’t have HtmlPane control in HTMLControls.

    I have a button to click inside a pane,
    browserWindowObject -> loginHtmlDocument -> pane -> loginButtonObj .

    as i am new to coded-ui not able to findthe rite way

    Tanks in advance

  8. Hi,

    in C#/CUIT/ Visual Studio…

    the Edit button is hidden, so couldnt automated using Record n play.. so i took xpath for Edit Button.. but am stuck with writing script for Edit Button.

    can you please help me.

Leave a Reply to Karthik kk Cancel reply

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