实现 UITestPropertyProvider 后,AccessibleName 仍然不是有效的 Searchproperty

After Implementing an UITestPropertyProvider, AccessibleName is still not a valid Searchproperty

我需要使用 Visual Studio 编码 UI 测试为 Delphi 应用程序实施自动 UI 测试。我已经为我的 Delphi-Contols 实现了 IAccessible 接口。它工作正常,我从控件中获取了 AccessibleName。

然后我实现了 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 测试生成器显示 AccessibleName 并且还在生成 AccessibleName 作为 SearchProperty。

UI地图:

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

*我在这里更改了搜索属性(仅针对 post,我没有更改生成的代码)

现在,当我开始测试时,我收到一个异常,提示 AccessibleName 不是有效的搜索属性。我之前遇到过这个异常,当时我还没有实现扩展。但是我认为通过实现 propertyprovider AccessibleName 现在应该是一个有效的搜索属性。 我尝试调试它,但似乎通过搜索控件它没有使用 propertyprovider,我不知道为什么?

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

保罗

我在使用有效搜索属性时遇到了问题。 我覆盖了 WinControl 中的 GetValidSearchProperties 方法。

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