How to test deep hierarchy controls using Coded UI Test in WPF?


In June 2011 we released an hot fix (KB2544407) addressing several issues that our customers are facing while using testing tools in Visual Studio 2010 Service Pack1. In that list one was related to Coded UI testing for WPF controls that are deep in hierachy. Following is the detailed information about the issue and how it is being fixed by the hot fix.

Some important links:

  • Brief information of all the issues fixed in the hotfix can be found here.
  • Download link for the hotfix can be found here.

Problem statement:

In WPF a control may be present under a different control at any nested level. In Coded UI Test we look for 2 levels up from control’s immediate ancestor [parent of the control in Coded UI Test] to see if there is any container control [e.g., Tab, ListBox, DataGrid]. If we don’t find it within 2 levels, no container control will be included in the Query ID. This will be an issue during playback, as we do not search inside a container control if it is not part of the Query ID for performance reasons. So if a container control exists above 2 or more levels from the control user acted on, playback will fail to find that control. The reason for going only 2 levels up to find the container control during recording is to void the performance impact and this number is chosen based on the testing done on real world applications scenarios. But we have been seeing going up to 2 levels is not sufficient some time. User may have some control which is nested at more than 2 levels deep under a container control.

What is the fix:

So to fix this, we have exposed a configuration key named ‘MaxLevelsForItemContainer” so that user can define the maximum level he is interested to look for an item container control in WPF applications. User can define this key in CodedUITestBuilder.exe.config/MTM.exe.config file as shown below. If the following key does not exist the default value still be 2 only. 

    <add key=”MaxLevelsForItemContainer” value=”<desiredLevel>”/>

How a user can figure out the no of levels:

In the sample application provided in this article, the hierarchy for the check box controls present inside the group items will be like as follows

Actual UI

UI hierarchy [using UISpy tool]

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

The container control [tab] is 3 levels up from the parent [group ‘flower1’] of the check box. If you try to record actions on the checkbox or create an assertion for the properties of checkbox control, following will be the hierarchy in Coded UI Test [picked up from Coded UI Test builder].

              

So it is clear from the above hierarchy that CheckBox control’s hierarchy does not have information related to tab item and tab control hence it is bound to fail when user runs the through Coded UI Test or Microsoft Test Manager.

Reason being while generating the hierarchy for the checkbox control, the recorder looks up to 2 levels from checkbox’s parent i.e., UIFlower1Group and hence no container control is found it comes out and the above hierarchy is generated.

Solution:

To fix the above issue, user first should try to find the max no of levels recorder should look up for container control using UISpy tool, in this case it is 3 [Flower1 (group)=> Flowers (group) => OnlineStore (TabItem) => Tab], and then update the
CodedUITestBuilder.exe.config or MTM.exe.config based on user’s need. A sample config file for CodedUITestBuilder.exe is attached within this article.
Following key should be added in config file under <appsettings> node.

    <add key=”MaxLevelsForItemContainer” value=”3″/>

Once the above is done, restart the CodedUITestBuilder and do the same action again on checkbox. This time user can see the complete hierarchy present for the control as shown below.

              

 

I have attached the sample configuration file for Coded UI Test builder and a sample application to reproduce the issue mentioned in the blog post. Users can use them to understand it better.

To understand more about WPF plugin in Visual Studio 2010 AFN and property validation refer the following deep dive sections on WPF support:

http://blogs.msdn.com/vstsqualitytools/archive/2010/04/13/uitest-framework-wpf-plugin-part-1.aspx

http://blogs.msdn.com/vstsqualitytools/archive/2010/04/13/uitest-framework-wpf-plugin-part-2.aspx

Happy testing!!!

Regards,

Deepak.

WPFDeepHierarchyControl.zip

Comments (15)

  1. Poonam says:

    This patch is working for Web application but for windows it is not working. Why So ?

  2. Hi Poonam

    This hotfix was released only for WPF controls. What type of windows application do you have? Is it winforms or WPF? For web you should see it working even without this patch.

    Regards,

    Deepak.

  3. Poonam says:

    Hi Deepak, Thank you very much for your quick reply.  I need your help.

    Actualy pages are all same for web and windows. It just deployment which is different. One is web application and another is desktop application but controls are same. They have used WPF and Telerik controls. For web version also I was getting control not found for controls inside tabs. After installing this patch it is getting recognised. But for desktop version still it is not.

    Please help me on this. Thanks a lot.

  4. Poonam says:

    Let me put into better way. The application is xbap and it has been deplyed as desktop using flexible template. For webversion controls inside tabs are getting recognised but for desktop version it is not. It says not found control.

    Please Help…

  5. It could be because of a custom control or something. Can you please share the UITest files of both projects [one that you created for Web app and another for winforms app]? My mail id is deepak.singhalATmicrosoftDOTcom

  6. Poonam says:

    I have added levels under appsettings as suggested by you and it is working. Thanks to you.

    But telerik ribbon controls are not getting recognised. It is not getting recorded by Coded UI.

    Any idea ?? Any workaround ?

    Thanks,

    Regards,

    Poonam

  7. JD612 says:

    This works fine for manual tests recorded and played back in MTM.

    If I want to run an automated test on a Virtual Machine where Test Agent is running, I still get the same problem. The controls cannot be found. I updated the Visual Studio Test agent on the Virtual Machine to SP1 and then installed the patch.

    But I cannot find the file where to add the <add key="MaxLevelsForItemContainer" value="<desiredLevel>"/>  on the virtual machine? Do I have to do this? Why is it not working? Please advise!

  8. Hi JD612.

    Apologies for replying back a little late here.

    This feature of enabling testing of deep hierarchy controls is mainly for the recording time [i.e., when actually you record your actions or create the Coded UI Test or a test automation in MTM].

    You may want to update the CodedUITestBuilder.exe.config file on the machine where you are creating coded UI Test and re-record the scenario. Once it is done you can run this test anywhere i.e., either on the same machine or on the machine having test agent.

    Please let me know if this doesn't help you.

    -Deepak.

  9. Hi Deepak,

    I am using RibbonBar control in my WPF application.But when I do playback it didn't recognised.Can you please help me ?

  10. Stephen says:

    It would be nice to include paths to where these are commonly located (the configs I mean).

  11. Adamsg says:

    I noticed the last post was in July of last year, but so I'm not sure if I should add to this.

    First, we have SP1 loaded, I added <add key="MaxLevelsForItemContainer" value="<desiredLevel>"/>  to the CodedUITestBuilder.exe.config file and this is a Windows App.

    We have a custom control over the treeview and I'm not sure if it is causing the issue.

    Similarly, when I record I'm getting the last node in the tree (ex. I get D3 if levels are A -> B1-> C12-> D3)

    I noticed that if I open the app, click on A, B1 and C12 it runs fine.

    The way we're testing, A,B1 and C12 need to be selected first. In the custom code clicking on A will load the Next level B1 thru B10. Then clicking on B1 will load the next level C1 thru C12.

    How do I get it to record the clicks on A, B1 and C12?

    I have added the clicks for A, B1 and C12 in the UIMap which works great so I have a work around. But someone else is creating the test app and I would like to make it as easy as I can since the custom Treeview is used a lot.

    Thanks for the help.

    Greg  

  12. Adamsg says:

    Never mind!

    I figured it out for myself, but I will post my solution in case someone else needs it.

    The issue in my case was that the Treeview nodes kept coming up with a ControlType of Tree, which is the Tree control. The nodes should be ControlType of TreeItem. In working through the application and a lot of reading on coded UI Test, I found that the Application.EnableVisualStyles() is important. When I uncommented this line of code, the tree nodes were visible when performing a coded UI Test.

    I hope this is helpful and hopefully I did not cost anyone else some time.

    Thanks,

    Greg

  13. Pablo Mauricio says:

    Greg

    Can you share how did you fix this in detail? I have the same problem.

  14. Teh says:

    Hi Deepak,

    The hotfix download link

    ( connect.microsoft.com/…/DownloadDetails.aspx ) is showing page not found.

    Could you please provide me alternate link to download hot fix KB2544407 ?

    Thanks.

  15. biju says:

    can someone provide the link for the hotfix?