Having trouble testing my WPF app with Coded UI Test

Problem: I cannot find my WPF TextBlock using the Coded UI test because the value is data bound and changes. Solution: Set the Name attribute on the controls of your View which sets the AutomationId. Explanation: Many WPF developers only place the Name attribute on items they intend to access from a code behind.  With most WPF developers using MVVM and trying to keep their Views as light as possible there would be no need to use the Name attribute.  However, if you intend to use Microsoft's new Coded UI test to test your view the Name attribute is important. The Coded UI test uses the Accessibility Framework to locate the controls on the screen.  The more distinct attributes the item has the more likely the test will be able to locate that control during playback. The Name attribute on WPF controls sets the AutomationId and allows it to be used to locate the control.  This is important when the value of for example a TextBlock changes.  If the Name attribute is not set the actual content of the TextBlock which is going to be changing during execution will make it almost impossible to locate that control during playback

How to reach nested controls using Coded UI Test Builder

Problem I have a span nested inside an anchor tag and I cannot reach it with CodedUI Test Builder. Solution Use the control navigation button in the Add Assertions dialog.   Explanation While trying to check the attributes on a link in a web application I could never see the font style to determine if it was bold or not.  After further investigation I realized the style I was looking for was not on the link but actually on the span inside the link.  However, whenever I used the cross-hair icon I was only able to select the hyperlink and never the span.  Then I noticed that there is a button in the Add Assertions dialog on the far right that allows you to move to other controls relative to the one currently selected.  Once I clicked the down arrow on the button I was moved to the nested span inside the hyperlink.  

How to change the name of a CodedUI method without Feature Pack 2.

  Problem: I want to change the name of a method generated by the Coded UI Builder. Solution: Modify the UI map and regenerate code. Explanation: I would suggest downloading Feature Pack 2. However, if you are not a MSDN subscriber you may not have access to FP2. When you record using the Coded UI Test Builder or use an Existing Action Recording you may end up with method names you wish you could change.  We have all been taught to never modify a .Designer file because those are generated an may change at any time.  So the key is to update the value in the source of the code generation. That is the .uitest file.  A .uitest file is an XML representation of the actions taken during the recording.  To change a method name you simply locate the TestStepMarkerAction element with the MarkerInformation attribute with the value of the method you wish to change.  Update the file with the desired method name then save and close the file.  Now we must cause the .Designer file to be regenerated to use the new method names.  From the Solution Explorer right click the .uitest file and select Edit with Coded UI Test Builder.  Once the Coded UI Test Builder opens simply click the Generate Code button and close the builder.  Now the .Designer file will contain the new method names.      

Demo Source Code

For those that wanted the demo code I presented today in the Web Cast it is here. It includes the code to databind a Coded UI Test. Thanks for joining me and I hope you enjoyed it. Demo.zip (408.41 kb)