This blog post has been updated to reflect changes in VS 2010 RC or later build. If you are on VS 2010 Beta 2, please upgrade before trying this post.
One question that keeps coming up is “How to configure search properties used by the recorder\code generation for identifying the control?”. For example, inform recorder that the Name of the certain control is dynamic and not to use it to identify the control.
Well, this feature per say is missing in VS 2010 release of Coded UI Test. However, there is a fairly simple workaround to this issue using the extensibility support of Coded UI Test. The configuring search properties may mean add or remove or change of search properties. Using the extensibility support, remove and change of search properties is possible but not add (unless you know the property value too).
The extensibility sample for this is attached to this post as RemoveUnwantedProperties.zip.
Before starting with this sample, ensure you have read at least the first two post on Coded UI Test Extensibility series.
Explaining the Sample –
- The sample hooks into UITest.Saving event. This event is raised before the UITest file is saved and code is generated for it.
- It then iterates through all the UI objects. The UI objects are stored in an hierarchical tree. The sample uses recursion to do pre-order traversal of the tree. (Check for the calls to RemoveUnwantedSearchCondition() method.)
- During traversal, for each UI object, it calls GetRedundantSearchProperties() method in the sample. Based on the property names returned by this method, it removes those properties, if present, from the UI object’s search criteria.
- The current implementation of GetRedundantSearchProperties() method simply removes “Name” property for all but few Win32\WinForms control from the search criteria. Depending on your need, you can customize this function accordingly.
Please note that since the sample is removing a search property, it might make the search criteria weak and result in playback failure. This is the reason for “claiming” this only a “workaround”.