【问题标题】:UI-Automation cmdlet not finding the controlUI-Automation cmdlet 找不到控件
【发布时间】:2011-02-09 20:38:52
【问题描述】:

我正在尝试使用 MSFT 提供的 UI-Automation 框架测试 WPF 应用程序。编写了一些 powershell 脚本,它们调用为操纵应用程序的可视控件而创建的 cmdlet。

我的应用程序中有一个 DropDown,它有一个条目“DropDownEntry”。在我的 cmdlet 中,我正在尝试执行以下操作:

 AutomationElement getItem = DropDown.FindFirst(TreeScope.Descendants,
 new AndCondition(
 new PropertyCondition(AutomationElement.ControlTypeProperty,ControlType.ListItem),
 new PropertyCondition(AutomationElement.NameProperty, "DropDownEntry",PropertyConditionFlags.IgnoreCase)));

上面给定的 sn-p 在执行时返回“null”,这基本上意味着上面给定的逻辑无法找到我的下拉条目。

谁能告诉我为什么会发生这种情况?我检查了我的控件的名称和值。一切似乎都井然有序。我不确定为什么会发生这种情况。任何帮助将不胜感激。

谢谢

【问题讨论】:

    标签: c# wpf testing powershell ui-automation


    【解决方案1】:

    由于它是您要自动化的 DropDown 控件,因此在下拉 DropDown 之前,子项可能无法通过 UIAutomation 获得。

    您需要从 DropDown 元素中获取ExpandCollapse 模式,然后调用它的 Expand 方法。

    我创建了一些扩展方法来帮助掌握模式。这是一个例子

    public static class PatternExtensions
    {
        public static ExpandCollapsePattern GetExpandCollapsePattern(this AutomationElement element)
        {
            return element.GetPattern<ExpandCollapsePattern>(ExpandCollapsePattern.Pattern);    
        }
    
        public static T GetPattern<T>(this AutomationElement element, AutomationPattern pattern) where T : class
        {
            object patternObject = null;
            element.TryGetCurrentPattern(pattern, out patternObject);
    
            return patternObject as T;
        }
    }
    

    像这样使用它:

    DropDown.GetExpandCollapsePattern().Expand()
    

    然后你可以执行你的原始代码来找到子元素。

    【讨论】:

    • @Samuel Jack - 谢谢。这有帮助
    【解决方案2】:

    如果您还没有,您可能需要使用 UISpy 检查您的应用程序以验证属性。

    【讨论】:

    • 谢谢 jws。我想知道我应该寻找什么样的房产?如果我尝试使用 UI-Automation 访问的控件未显示在 Visual 树中,我是否应该提高标志?理论上 UI 自动化是否可以访问应用程序中可见的任何内容?
    • 这取决于应用程序,但如果它使用标准 Windows 控件构建,那么所有内容都应该可以通过 UIAutomation 访问。例如,Firefox 使用 XUL 和 Gecko,并且无法访问 UIAutomation。如果您没有在可视化树中看到下拉控件,则它不是 UIAutomationElement。如果您没有看到下拉条目,请查看@Samuel Jack 的回答。
    猜你喜欢
    • 2016-11-01
    • 2018-10-26
    • 2019-07-05
    • 1970-01-01
    • 1970-01-01
    • 2013-11-04
    • 2016-12-09
    • 2013-07-23
    • 2017-09-07
    相关资源
    最近更新 更多