Geeks With Blogs

News Hi, my name is Vincent Grondin and I'm a senior consultant at Fujitsu, a consulting firm in Montreal, Québec. I'm starting that blog to share some of my thoughts and knowledge on .NET architectures and code. Being a consultant in the .NET world I come across many different things. Some good, some bad and others that are worth a blog post here whether they be good, or hmmm... shall I say, less good :) I hope you enjoy yourself while learning new stuff. Feel free to leave a comment or contact me anytime.
Vincent Grondin

Not so long ago on our project at work we had to create shared steps in Microsoft Test Manager
for playback later on.  The screen we used contained 2 instances of the same custom made combobox
both displaying a list of countries.  One was located at the top of the screen and the other one at the bottom.
The combobox already supported Automation IDs and could " auto-magically " generate the right
automation id for each entry to be displayed in the list portion of the combobox according
to the key of each element to display, in this case, the country name.  Remember that this portion
of the control is VIRTUALIZED.

Now, the interesting part... We record our test in MTM and in this simple test, we pick a country from
the list of countries at the bottom of the screen.  We had done enough recording and coded ui tests
on this control to know it worked great... When came the playback time, we saw our recording do exactly
what it was supposed to do, pick the country we had selected EXCEPT, it did in in the upper combobox instead
of the bottom one!  HMMM.... How weird!  Both controls had different Automation IDs but had the same automation
ids for the virtualized content because they both displayed the same kind of information being, countries...

OK so the solution was simple, concatenate the controls unique Automation ID with the
unique content for each virtualized row...

The code behind for the control overrides PrepareContainerForItemOverride like so:

 //Declare this property inside your control and initialize it inside your constructor
 public BindingBase BindingAutomationId { get; set; }

 protected override void PrepareContainerForItemOverride(DependencyObject element, Object item)
            base.PrepareContainerForItemOverride(element, item);
            DataGridRow row = element as DataGridRow;

            //Can't put these lines in the constructor because the GetAutomationId call returns NULL consistantly from inside it
  //The magic is here
                this.BindingAutomationId.StringFormat = AutomationProperties.GetAutomationId(this) + "_{0}";
            row.SetBinding(AutomationProperties.AutomationIdProperty, BindingAutomationId);

So now each virtualized content inside this control has a unique automation id and the playback works perfectly.

Hope this saves you a ton of time trying to figure out why your playback won't pick the control you selected during the recording phase.


Posted on Saturday, January 5, 2013 9:34 PM | Back to top

Comments on this post: How I got burned with Automation Ids and virtualized content in XAML controls...

No comments posted yet.
Your comment:
 (will show your gravatar)

Copyright © Vincent Grondin | Powered by: