Having trouble testing my WPF app with Coded UI Test

by Donovan Brown 17. September 2011 20:44

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

Tags: , , ,

Work

About the author

My name is Donovan Brown and I am a process consultant for Imaginet with a background in application development.  I also run one of the Nation’s fastest growing online registration sites for motorsports events DLBRacing.com.  When I am not writing software I race cars for fun.  DLBRacing.com has given me the opportunity to combine my two passions writing software and racing cars.

AdSense

Month List

AdSense