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

Comments are closed