2

我正在尝试使用 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 所以我什至无法开始对其运行测试。关于我可以做些什么来解决这个问题的任何建议?

4

2 回答 2

2

看起来您可能需要在 XAML中设置 AutomationId 属性才能显示 - 它看起来不像 Name 属性被公开为 AutomationId。

您可能还想使用检查工具检查元素是否实际暴露在自动化树中,并具有您期望的 AutomationId 或其他属性。

于 2012-06-01T06:38:47.667 回答
0

请参阅下面的线程检查,您需要为每个控件使用 AutomationId 来获取使用它的元素。

http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/b0892b5c-3850-4518-8063-d4eb5a8d9781/

于 2012-12-25T11:08:20.597 回答