【问题标题】:Should I be using AutomationPeer or AutomationElement? Or both?我应该使用 AutomationPeer 还是 AutomationElement?或两者?
【发布时间】:2015-06-27 02:12:56
【问题描述】:

我刚刚开始使用 AutomationElement,因为我们想对自定义控件进行集成测试,我认为我应该使用 AutomationElement。

我已经成功创建了一个包含自定义控件的Window,并且可以成功获取该窗口和控件的AutomationElements

  // Retrieve the View
  System.Windows.Automation.Condition viewCondition = new PropertyCondition(AutomationElement.AutomationIdProperty, "MyTestView");
  AutomationElement view = AutomationElement.RootElement.FindFirst(TreeScope.Children, viewCondition);
  Assert.IsNotNull(view);

  // Retrieve the CustomControl
  System.Windows.Automation.Condition comboboxCondition = new PropertyCondition(AutomationElement.AutomationIdProperty, "MyCustomControl");
  AutomationElement combobox = view.FindFirst(TreeScope.Children, comboboxCondition);
  Assert.IsNotNull(comboboxCondition);

现在,我想做的是使用,例如 ValuePattern。这就是我感到困惑的地方。

为了寻找信息,我在 referencesource.microsoft.com 上搜索了 WPF 源。遇到了ComboboxAutomationPeer,它实现了IValueProvider,所以现在一头雾水。

我应该实现实现 IValueProvider 的 MyCustomControlAutomationPeer,然后 AutomationElement 将与 ValuePattern 一起使用吗?还是应该让 MyCustomControl 实现 IValueProvider?

【问题讨论】:

  • 一个 AutomationPeer 存在于您尝试自动化的应用程序中。就像 WPF 应用程序中的 ComboBox 一样。 AutomationElement 是您在代码中使用的使用 自动化来修补此类 WPF 应用程序的元素。这段代码。
  • 感谢您解释汉斯的区别。

标签: c# wpf ui-automation microsoft-ui-automation


【解决方案1】:

您无需实现任何东西即可使用模式。 UI 自动化为您执行此操作(充当目标应用程序的代理)。这在官方文档中有很好的解释:Get Supported UI Automation Control Patterns

这是一个示例摘录:

    SelectionItemPattern pattern;
    try
    {
        pattern = yourAutomationElement.GetCurrentPattern(SelectionItemPattern.Pattern) as SelectionItemPattern;
    }
    catch (InvalidOperationException ex)
    {
        Console.WriteLine(ex.Message);  // Most likely "Pattern not supported." 
        return;
    }
    pattern.Select();

【讨论】:

  • 好的,我现在要更具体一些:假设我有一个由模板部分组成的自定义控件。你是说这个自定义控件会自动拥有其部件的模式? IE。我不必在我的自定义控件中实现任何东西来执行 UI 自动化,因为它会继承文本框部分的模式?
  • 好的,现在您说的是使用 AutomationPeers 的服务器端(我的示例是客户端)。对于服务器端,一般而言,您无需执行任何操作,因为 WPF 会自动为标准控件构建对等点。但是,如果您需要添加一些自定义行为(例如,如果您有一个纯绘图但想要突出显示某些部分),那么您可以。但是您应该首先使用 UIA sdk 工具(inspect.exe 等)检查您的控件公开的现成内容
  • 我没有标准控件。我有一个带有模板部分的自定义控件。我的自定义控件是否继承了模板部分的模式?
  • 是的。但就像我说的,只需尝试并使用 SDK 中的 Inspect,您就会看到它是否被看到以及如何看到、支持哪些模式等。
  • 感谢您耐心的回复,我周一回来工作,将继续我的 AutomationElement 和 ValuePattern 实验。 (届时将接受您的回答)
猜你喜欢
  • 1970-01-01
  • 2016-03-14
  • 2017-08-29
  • 1970-01-01
  • 2019-01-22
  • 1970-01-01
  • 2019-10-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多