我正在尝试使用 UI 自动化框架测试一些自定义控件。我的一个控件有一个 TextBox 基类,另一个继承自 Control。我可以通过测试找到我的第一个控件,但是无论我使用 TreeScope 和属性条件的哪种组合,我都无法在窗口中找到我的第二个自定义控件。
我在 XAML 中声明自定义控件,如下所示:
<Grid>
<test:CustomTextBox Height="23" HorizontalAlignment="Left" Margin="12,12,0,0" Name="customTextBox1" VerticalAlignment="Top" Width="120" />
<test:CustomUserControl Height="25" HorizontalAlignment="Left" Margin="12,62,0,0" Name="customUserControl1" VerticalAlignment="Top" Width="119" />
</Grid>
我有一个像下面这样的样本测试。
[Test]
public void TestUsingValuePattern()
{
// Getting RootElement...
AutomationElement rootElement = AutomationElement.RootElement;
Assert.IsNotNull(rootElement);
// Searching for Test Window...
AutomationElement windowElement = rootElement.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.NameProperty, "TestWindow"));
Assert.IsNotNull(windowElement);
// Searching for Custom TextBox control...
AutomationElement customElement1 = windowElement.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.AutomationIdProperty, "customTextBox1"));
Assert.IsNotNull(customElement1);
// Searching for Custom User control
AutomationElement customElement2 = windowElement.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.AutomationIdProperty, "customUserControl1"));
Assert.IsNotNull(customElement2);
}
第二个断言总是返回 null 所以我什至无法开始对其运行测试。关于我可以做些什么来解决这个问题的任何建议?