In one of my earlier blogs here, I had explained the some search failure scenario and how to go about tackling the problem. As a continuation to that, I’m providing a simple application scenario and the solution here.

 

My test scenario is to click on a tree item “Sub Item #1” for a simple WPF app as shown in the snapshot below:

 WPF App Snapshot

 

Open a Coded UI Test project, and record a click on the “Sub Item #1” Tree Item. Your recorded method will show up something like –

 

        public void RecordedMethod1()

        {

            #region Variable Declarations

            WpfTreeItem uISubItem1TreeItem = this.UIMainWindowWindow.UIItemCustom.UIItemTree.UIRootTreeItem.UIItem5TreeItem.UISubItem1TreeItem;

            #endregion

 

            // Click 'Root' -> 'Item #5' -> 'Sub Item #1' tree item

            Mouse.Click(uISubItem1TreeItem, new Point(38, 8));

        }

 

When you run the test, it will fail due to a search failure.

 

Test method SearchHierarchyFix.CodedUITest1.CodedUITestMethod1 threw exception:

Microsoft.VisualStudio.TestTools.UITest.Extension.UITestControlNotFoundException: The playback failed to find the control with the given search properties. Additional Details:

TechnologyName:  'UIA'

ControlType:  'TreeItem'

Name:  'Sub Item #1'

 Search may have failed at '' TabList as it may have virtualized children. If the control being searched is descendant of '' TabList then including it as the parent container may solve the problem. ---> System.Runtime.InteropServices.COMException: Error HRESULT E_FAIL has been returned from a call to a COM component.

 

Going by the description I provided in the previous blog, the issue over here is that the search failed even before it reached the target element i.e. uISubItem2TreeItem. It failed at an intermediate element itself, in this case 'UIItemCustom'. The reason being, the ancestor TabList and TabPage controls are not being auto-generated as part of the search hierarchy. The ideal search hierarchy should have been

this.UIMainWindowWindow.UIItemTabList.UITab1TabPage.UIItemCustom.UIItemTree.UIRootTreeItem.UIItem5TreeItem.UISubItem2TreeItem

instead of

this.UIMainWindowWindow.UIItemCustom.UIItemTree.UIRootTreeItem.UIItem5TreeItem.UISubItem2TreeItem

 

So how do we fill the missing 'UIItemTabList.UITab1TabPage'?

 

1.       Launch the Coded UI Test builder once again.

2.       Locate the “Tab #1” control by moving the cross hair over it.

3.       Add it to the UI object hierarchy. Close the Coded UI Test builder.

4.  In your test method, insert the following line of code before Mouse.Click(uISubItem1TreeItem, …)

 

            this.UIMainWindowWindow.UIItemCustom.Container = this.UIMainWindowWindow.UIItemTabList.UITab1TabPage;

 

This essentially does what I described in the blog. It stitches the search tree to include the UIItemTabList & UITab1TabPage as the ancestor of UIItemCustom.

(Unlike what I mentioned in the blog, you do not actually have to do an additional UITab1TabPage.Container = this.UIMainWindowWindow.UIItemTabList since the uITab1TabPage already has a good search hierarchy of its own.)

The missing link was making the search from UIMainWindowWindow to UIItemCustom work successfully, which we achieved by inserting this UIItemTabList.UITab1TabPage in between them by using the .Container property.

 

I’ve attached the test application and the Coded UI Test solution for reference.