Coded UI Test supports “Intent Aware Recording” i.e. the recording attempts to capture the intent of the user instead of the raw SendKeys or MouseClick actions. In order to achieve this, Coded UI Test has in-built Action Filters that process the raw actions based on a set of pre-defined rules and generate the set of aggregated actions.

 

For example,  in case of  Silverlight List Box, the selection of an item gets aggregated into a SetValue action on the container List Box. The Action Filter Rule is quite straight forward –

 

Rules:

·         Mouse Click action on a List Item ‘X’ precedes a SetValue action on a List Box ‘Y’.

·         The List Box ‘Y’ is the immediate parent of the List Item ‘X’

·         The SetValue on ListBox ‘Y’ was actually caused by selection of List Item ‘X’

Action:

·         Remove the Mouse Click action on the List Item ‘X’.

 

For certain Silverlight List Box and Combo Box controls, the aggregated action may not be generated during recording; instead the raw click actions are recorded. This behavior will be seen when the Item control is customized and/or has nested composite content (instead of the default Text Block control). The default hierarchy for a List Item will be something like

 

List Box

|__ ListBoxItem

         |__ Text Block

 

Say the LisBoxItem controls are customized by applying a ItemContainerStyle as –

 

        <ListBox Name="listBox1" ItemsSource="{Binding Source={StaticResource employees}}">

            <ListBox.ItemContainerStyle>

                <Style TargetType="ListBoxItem">

                    <Setter Property="Template">

                        <Setter.Value>

                            <ControlTemplate TargetType="ListBoxItem">

                                <Grid>

                                    <Image Source="{Binding EmployeePicUri}" Width="20" />

                                    <TextBlock Content="{Binding EmployeeName}"/>

                                </Grid>

                            </ControlTemplate>

                        </Setter.Value>

                    </Setter>

                </Style>

            </ListBox.ItemContainerStyle>        

        </ListBox>

 

The visual tree hierarchy will now be something like –

 

List Box

|__ ListBoxItem

         |__ Image

         |__ Text Block

 

Any mouse click action on the ListBoxItem (which will now be acted upon the Image or the TextBlock) will *not* be aggregated out to a SetValue action on the ListBox.

 

The internal heuristic (that decides on whether to record a SetValue action on the List Box or not) is –

 

·         Check whether there is at least one TextBlock control as a child of the ListItem

·         AutomationProperty.Name of the TextBlock control  is same as the AutomationProperty.Name of the ListItem.

 

So in case you want to ensure Coded UI Test records the SetValue action, make sure the control adheres to the above mentioned condition.

 

If the control template cannot be modified to align to this check, the other alternative is to write a custom Action Filter that converts the Mouse Click action on the composite control inside the List Item into a SetValue action on the ListBox. The post here explains how to write an Action Filter extension in Coded UI Test.