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
- Identify the Div name (It can be for e.g. HeaderContent)
- 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)
- 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.
- 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);
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
20 Replies to “How to identify all the child elements within a page in Coded UI Test (CUIT)”
its helped me a lot,
can you please send any doc u have for Coded UI related things
The posts are my docs, I keep no doc with me, everything is out @ your disposal.
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 :/
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
If you follow the link 2 guidelines, the code will be faster.
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,
Try to change the IE driver with ChromeDriver, it should work fine. Nothing to do with Console.Writeline.
I have a Pane, under that pane there is some childern. it looks like below
———- (First name)
Here i want to click on “Third name” , may i know how to do a wrapper class for this please?
Hi Karthik i am not able to click
this is page data, so i want to click
this tag id value in automation Testing
You are simply awesome.
This comment is for entire series.
Awesome, Awesome and Awesome
Thank you !!!
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
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
i want to search a string in entire page using coded ui. can you help me?
Just get the pagesource and search page in coded ui.
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?
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”;
Playback.PlaybackSettings.WaitForReadyLevel = WaitForReadyLevel.AllThreads;
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?
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 ?
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
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.