【发布时间】:2016-11-07 15:38:36
【问题描述】:
我开始使用 TestStack White(UI 自动化)来自动化 WPF 现有应用程序中的测试。使用标准控件时,一切正常。但是,我在尝试与自定义控件交互时遇到了问题。
例如,我有一个 LabeledComboBox,它实际上是一个 TextBlock 加上一个 ComboBox。这被定义为从 Control 派生的类加上 XAML 中的 ControlTemplate:
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>
此控件有效,但如果您运行 UI 自动化验证 UI 自动化可见的唯一部分是 ComboBox,并且无法访问 TextBlock。
但是,如果您使用 XAML 和代码隐藏将其创建为 UserControl,则 TextBox 和 ComboBox 对 UI 自动化都是正确可见的。
我已尝试为我的控件创建一个 AutomationPeer (FrameworkElementAutomationPeer),但到目前为止,我无法使 TextBlock 对 UI 自动化可见。一个有趣的结果是 FrameworkElementAutomationPeer::GetChildrenCore() 正确返回了 2 个自动化对等点的列表,一个用于 TextBlock,一个用于 ComboBox。
我应该如何更改我的自定义控件,以便使用 UI 自动化和 White 对其进行正确测试?
【问题讨论】:
标签: c# wpf custom-controls microsoft-ui-automation white-framework