Accessing some "unreachable" WPF controls in Coded UI Test


Recently we got some feedback from a customer that they are not able to access some control inside a datagrid cell. Basically they wanted to do some validation on this control’s properties. Coded UI Test uses the UIAutomation’s ControlView TreeWalker to do all UI tree navigation. Hence, if a control is not present in the ControlView, it will be unreachable i.e. neither you can navigate to that control using the CUIT’s control locator, nor can you search for it by providing any combination of  properties. The RawView TreeWalker could give some additional set of controls. CUIT excludes them since typically these are non-interesting controls and adds too much of verbosity to the CUIT control tree structure. 

Unfortunately, this TreeWalker is not currently configurable to the CUIT user. However, all is not lost. You can still do some limited operations on these “elusive” controls. The following code snippet gets the native AutomationElement from the UITestControl and then uses the RawView tree walker to drill down to a child control (which is otherwise inaccessible in the ControlView tree).

UITechnologyElement cellElement = cellUITestControl.GetProperty(UITestControl.PropertyNames.UITechnologyElement) as UITechnologyElement 

AutomationElement ae = cellElement.NativeElement as AutomationElement;

AutomationElement child = System.Windows.Automation.TreeWalker.RawViewWalker.GetFirstChild(ae);

Assert.AreEqual(child.Current.Name, “TheChildControlName”);

Note that you need to perform all property validations (or any actions such as using UIA Patterns to do some actions, or navigating further with this raw child control as reference) on the control directly using the UIAutomation APIs. You should not create back a UITestControl out of the ‘child’ AutomationElement above since it will be unreachable during the playback search.

Comments (5)

  1. ft says:

    I am new to CodedUI. Currently, we are experiencing an issue where the corsshair is not able to detect the text field, buttons inside the WPF window.

    I was wondering if the the snippet above will be able to help us solve our issue.

    My question would be, how do you use the code snippet above? Where do we insert it?

    Thanks.

  2. Tapas Sahoo says:

    Sorry about the late response. I didn't get your question entirely. Are these text/button controls contained inside any control such as Cell, or are they directly under the window? You need to get a reference to the parent control of the text/button which is recognizable by Coded UI Test, say UIObjectX. Then using 'X', you can drill down to the text field/button control using some similar approach that I've mentioned above.

    I would suggest post your queries to social.msdn.microsoft.com/…/threads

  3. Here is the another Reference which has example of using CacheRequest and RawView

    msdn.microsoft.com/…/system.windows.automation.cacherequest.aspx

  4. Hi,

    I am too facing the same issue. Coded UI Test failing to find some of WPF controls present inside a TabControl, so I prefer writing my own customised code to get catch of the control.

    Can someone guide me of where to write my code. I am using Coded UI Test 2012 to test my WPF Xbap application. In my case I have to traverse my TabControl element still more deeper to identify the other controls present inside it.

    Regards,

    Nivetha

  5. tiny apps says:

    it works perfect when I tried to get access to a textBlock control inside ControlType: UIA_ThumbControlTypeId (0xC36B) CodedUI testbuilder is not able to reach to this control by default

    cool,

    thanks for quick code