实现 UITestPropertyProvider 后,Accessible Name 仍然不是有效的 Searchpr

本文关键字:有效 Searchpr UITestPropertyProvider Accessible Name 实现 | 更新日期: 2023-09-27 18:37:01

我需要使用 Visual Studio 编码的 UI 测试为 Delphi 应用程序实现自动 UI 测试。我已经为我的德尔菲-康托尔实现了 IAccessible 接口。它工作正常,我从控件中获取可访问名称。

然后我为Visual Studio实现了扩展。在这个扩展中,我有自己的 PropertyProvider-、ExtensionPackage- 和 WinControl-Class。

物业提供者:

namespace CUITExtension
{
    public class AccessibleNamePropertyProvider : UITestPropertyProvider
    {    
        private static Dictionary<string, UITestPropertyDescriptor> accessibleNamePropertyMap = null;
        private static Dictionary<string, UITestPropertyDescriptor> AccessibleNamePropertyMap
        {
            get
            {
                if (accessibleNamePropertyMap == null)
                {
                    UITestPropertyAttributes read = UITestPropertyAttributes.Readable
                        | UITestPropertyAttributes.DoNotGenerateProperties;
                    accessibleNamePropertyMap = new Dictionary<string, UITestPropertyDescriptor>
                            (StringComparer.OrdinalIgnoreCase);
                    accessibleNamePropertyMap.Add("AccessibleName", new UITestPropertyDescriptor(typeof(string), read));
                }
                return accessibleNamePropertyMap;
            }
        }
        public override UITestPropertyDescriptor GetPropertyDescriptor(UITestControl uiTestControl, string propertyName)
        {
            return AccessibleNamePropertyMap[propertyName];
        }
        public override ICollection<string> GetPropertyNames(UITestControl uiTestControl)
        {
            if (uiTestControl.ControlType.NameEquals("Custom"))
            {
                // the keys of the property map are the collection of property names
                return AccessibleNamePropertyMap.Keys;
            }
            throw new NotSupportedException();
        }
        public override object GetPropertyValue(UITestControl uiTestControl, string propertyName)
        {
            if (String.Equals(propertyName, "AccessibleName", StringComparison.OrdinalIgnoreCase))
            {
                object[] native = uiTestControl.NativeElement as object[];
                IAccessible acc = native[0] as IAccessible;
                return acc.accName;
            }
            throw new NotSupportedException();
        }
        public override int GetControlSupportLevel(UITestControl uiTestControl)
        {
            if (string.Equals(uiTestControl.TechnologyName, "MSAA",
                StringComparison.OrdinalIgnoreCase) &&
                uiTestControl.ControlType.NameEquals("Custom"))
            {
                return (int)ControlSupport.ControlSpecificSupport;
            }
            // This is not my control, so return NoSupport
            return (int)ControlSupport.NoSupport;
        }
        public override string[] GetPredefinedSearchProperties(Type specializedClass)
        {
            return null;
        }
        public override string GetPropertyForAction(UITestControl uiTestControl, UITestAction action)
        {
            return null;
        }
        public override string[] GetPropertyForControlState(UITestControl uiTestControl, ControlStates uiState, out bool[] stateValues)
        {
            stateValues = null;
            return null;
        }
        public override Type GetPropertyNamesClassType(UITestControl uiTestControl)
        {
            if (uiTestControl.ControlType.NameEquals("Custom"))
                return typeof(AccessibleControl.PropertyNames);
            return null;
        }
        public override Type GetSpecializedClass(UITestControl uiTestControl)
        {
            if (uiTestControl.ControlType.NameEquals("Custom"))
                return typeof(AccessibleControl);
            return null;
        }
        public override void SetPropertyValue(UITestControl uiTestControl, string propertyName, object value)
        {
            return;
        }
    }
}

扩展包:

[assembly: Microsoft.VisualStudio.TestTools.UITest.Extension.UITestExtensionPackage(
                "AccessibleNameExtensionPackage",
                typeof(CUITExtension.AccessibleNameExtensionPackage))]
namespace CUITExtension
{
    class AccessibleNameExtensionPackage : UITestExtensionPackage
    {
        public override string PackageDescription
        {
            get { return "Supports coded UI testing by using the AccessibleName"; }
        }
        public override string PackageName
        {
            get { return "AccessibleName Extension Package"; }
        }
        public override string PackageVendor
        {
            get { return "Microsoft (sample)"; }
        }
        public override Version PackageVersion
        {
            get { return new Version(1, 0); }
        }
        public override Version VSVersion
        {
            get { return new Version(14, 0); }
        }
        public override void Dispose() { }
        public override object GetService(Type serviceType)
        {
            if (serviceType == typeof(UITestPropertyProvider))
            {
                if (propertyProvider == null)
                {
                    propertyProvider = new AccessibleNamePropertyProvider();
                }
                return propertyProvider;
            }
            return null;
        }
        private UITestPropertyProvider propertyProvider = null;
    }
}

WinControl:

namespace CUITExtension
{
    public class AccessibleControl : WinControl
    {
        public AccessibleControl(UITestControl c) : base(c)
        {
            TechnologyName = "MSAA";
            SearchProperties.Add(UITestControl.PropertyNames.ControlType, "Custom");
        }
        public virtual string AccessibleName
        {
            get
            {
                return (string)GetProperty("AccessibleName");
            }
        }
    }
}

现在,编码的 UI 测试生成器显示辅助名称,并且还将辅助名称生成为搜索属性。

UIMap:

public AccessibleControl UIItemCustom
        {
            get
            {
                if ((this.mUIItemCustom == null))
                {
                    this.mUIItemCustom = new AccessibleControl(this);
                    #region Search Criteria
                    this.mUIItemCustom.SearchProperties["AccessibleName"] = "UniqueName1";
                    this.mUIItemCustom.SearchProperties[WinControl.PropertyNames.ClassName] = "TEdit";
                    this.mUIItemCustom.WindowTitles.Add("Title");
                    #endregion
                }
                return this.mUIItemCustom;
            }
        }

*我在这里更改了搜索属性(仅用于帖子,我没有更改生成的代码)

现在,当我开始测试时,我收到一个异常,指出 Accessible Name 不是有效的搜索属性。当我还没有实现扩展时,我之前遇到了这个异常。但是通过实现属性提供程序 AccessName 现在应该是一个有效的搜索属性。我试图调试它,但似乎通过搜索控件它不使用属性提供程序,我不知道为什么?

我希望你能帮助我,如果你需要更多信息,请问。

保罗

实现 UITestPropertyProvider 后,Accessible Name 仍然不是有效的 Searchpr

我遇到了有效搜索属性工作的问题。我覆盖了WinControl的GetValidSearchProperties方法。

protected override Dictionary<string, bool> GetValidSearchProperties()
        {
            Dictionary<string, bool> searchProperties = base.GetValidSearchProperties();
            if (!searchProperties.ContainsKey("AccessibleName"))
                searchProperties.Add("AccessibleName", true);
            return searchProperties;
        }