简体   繁体   English

UI自动化cmdlet找不到控件

[英]UI-Automation cmdlet not finding the control

I am trying to test a WPF application using the UI-Automation framework that MSFT provides. 我正在尝试使用MSFT提供的UI自动化框架来测试WPF应用程序。 There were a few powershell scripts written that invoked the cmdlets created to manipulate the visual controls of the application. 编写了一些powershell脚本,这些脚本调用了为操纵应用程序的可视控件而创建的cmdlet。

There is a DropDown within my application that has an entry 'DropDownEntry'. 我的应用程序中有一个DropDown,其中有一个条目“ DropDownEntry”。 In my cmdlet, I am trying to do something as follows: 在我的cmdlet中,我尝试执行以下操作:

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

The above given snippet returns 'null' upon execution which essentially means that the above given logic was unable to find my dropdown entry. 上面给出的代码片段在执行时返回“ null”,这实际上意味着上面给出的逻辑无法找到我的下拉条目。

Can somebody tell me why this might be happening? 有人可以告诉我为什么会这样吗? I checked the name of my control and the values. 我检查了控件的名称和值。 Everything seems to be in order. 一切似乎都井井有条。 I am not sure why this would be happening. 我不确定为什么会这样。 Any help would be much appreciated. 任何帮助将非常感激。

Thanks 谢谢

Since it is a DropDown control you are automating, it may be that the child items are not available through UIAutomation until the DropDown is dropped down. 由于这是您要自动执行的DropDown控件,因此可能只有在DropDown下拉之前,子项才可以通过UIAutomation使用。

You need to get hold of the ExpandCollapse pattern from the DropDown element, then call its Expand method. 您需要从DropDown元素获取ExpandCollapse模式,然后调用其Expand方法。

I created some extension methods to help with getting hold of patterns. 我创建了一些扩展方法来帮助掌握模式。 Here's one example 这是一个例子

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;
    }
}

Use it like this: 像这样使用它:

DropDown.GetExpandCollapsePattern().Expand()

Then you can execute your original code to find the child element. 然后,您可以执行原始代码以找到子元素。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM