Blog Stats
  • Posts - 47
  • Articles - 0
  • Comments - 21
  • Trackbacks - 0

 

How I got burned with Automation Ids and virtualized content in XAML controls...

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
            if(string.IsNullOrEmpty(this.BindingAutomationId.StringFormat))
            {
  //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.

 


Feedback

No comments posted yet.


Post A Comment
Title:
Name:
Email:
Comment:
Verification:
 
 

 

 

Copyright © Vincent Grondin