I am beginning to use TestStack White (UI Automation) to automate tests in an WPF existing application. When using standard controls everything works fine. However, I run into problems when trying to interact custom controls.
For example, I have a LabeledComboBox which is actually a TextBlock plus a ComboBox. This is defined as a class derived from Control plus a ControlTemplate in XAML:
public class LabeledComboBox : Control
{
static LabeledComboBox()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(LabeledComboBox), new FrameworkPropertyMetadata(typeof(LabeledComboBox)));
}
}
<local:LabeledComboBox>
<local:LabeledComboBox.Template>
<ControlTemplate TargetType="{x:Type local:LabeledComboBox}">
<StackPanel>
<TextBlock Text="Text"/>
<ComboBox/>
</StackPanel>
</ControlTemplate>
</local:LabeledComboBox.Template>
</local:LabeledComboBox>
This control works, but if you run UI Automation Verify the only part visible to UI Automation is the ComboBox, and the TextBlock cannot be accessed.
However, if you create this as a UserControl using XAML and Code Behind, both the TextBox and the ComboBox are properly visible to UI Automation.
I have tried to create an AutomationPeer (FrameworkElementAutomationPeer) for my control, but I have not been able to make the TextBlock visible to UI Automation so far. One interesting result is that FrameworkElementAutomationPeer::GetChildrenCore() properly returns a list of 2 automation peers, one for the TextBlock and one for the ComboBox.
How should I change my custom control so it is properly testable using UI Automation and White?