代码之家  ›  专栏  ›  技术社区  ›  Anthony Mastrean PavanDevarakonda

如何从uitestcontrol获取itemstatus?

  •  2
  • Anthony Mastrean PavanDevarakonda  · 技术社区  · 17 年前

    这个 UI Automation framework 有一个基类, AutomationElement 有财产的, ItemStatus ,可用于存储任意字符串。我正试图从Visual Studio 2010中获取该属性 Coded UI Tests 基类, UITestControl .

    1 回复  |  直到 17 年前
        1
  •  2
  •   Anthony Mastrean PavanDevarakonda    17 年前

    查看代码化的UI测试生成的代码 WpfControl . 它有一个属性nativeElement。此属性是 AutomationElement .

    public abstract class WpfControl : UITestControl
    {
        ...
    
        public virtual object NativeElement
        {
            get
            {
                return ((object)(this.GetProperty(UITestControlProperties.Common.NativeElement)));
            }
        }
    
        ...
    }
    

    您可以编写一个扩展方法来强制转换它并获取itemstatus。

    public static string GetItemStatus(this WpfControl control)
    {
        var automationElement = (AutomationElement)control.NativeElement;
        return automationElement.Current.ItemStatus;
    }
    

    我不确定为什么NativeElement被记录为 object (这使得getter投射多余)。所有WPF控件的NativeElement的类型都是 自动元件 . 我建议编辑生成的代码并简单地调用 control.NativeElement.Current.ItemStatus 直接。