IValueProvider 的实现导致 UITestControlNotAvailableException

Implementation of IValueProvider leads to UITestControlNotAvailableException

我在 UI WindowsStore 应用程序的自动化方面遇到了一些困难。我在实施 IValueProvider 后立即收到 UITestControlNotAvailableException。如果我删除它的实现。

StackTrace 如下所示:

at Microsoft.VisualStudio.TestTools.UITest.Extension.Uia.UiaUtility.MapAndThrowException(Exception e, IUITechnologyElement element, Boolean useRethrowException)
at Microsoft.VisualStudio.TestTools.UITest.Extension.Uia.UiaUtility.GetAutomationPropertyValue[T](AutomationElement element, AutomationProperty property)
at Microsoft.VisualStudio.TestTools.UITest.Extension.Uia.UiaElement.GetAutomationPropertyValue[T](AutomationProperty automationProperty)
at Microsoft.VisualStudio.TestTools.UITest.Extension.Uia.UiaElement.get_NativeWindowHandle()
at Microsoft.VisualStudio.TestTools.UITest.Extension.Uia.UiaElement.get_WindowHandle()
at Microsoft.VisualStudio.TestTools.UITesting.UITestControl.get_IsRefetchRequired()
at Microsoft.VisualStudio.TestTools.UITesting.UITestControl.EnsureValid(Boolean waitForReady, Boolean refetch)
at Microsoft.VisualStudio.TestTools.UITesting.UITestControl.GetPropertyValuePrivate(String propertyName)
at Microsoft.VisualStudio.TestTools.UITesting.UITestControl.GetPropertyPrivate(String propertyName)
at Microsoft.VisualStudio.TestTools.UITesting.UITestControl.GetPropertyOfType[T](String propertyName)
at Microsoft.VisualStudio.TestTools.UITesting.UITestControl.get_Name()
at UITest.Framework.Windows.TestGround.GetElementString(UITestControl element, Int32 level) in ...
at UITest.Framework.Windows.TestGround.WalkTree(UITestControl element, Int32 level) in ...
at UITest.Framework.Windows.TestGround.WalkTree(UITestControl element, Int32 level) ...
at UITest.Framework.Windows.TestGround.WalkTree(UITestControl element, Int32 level) ...
at UITest.Framework.Windows.TestGround.WalkTree(UITestControl element, Int32 level) ...
at UITest.Framework.Windows.TestGround.PrintTree(Modes mode, XamlWindow window) in ...

在应用程序端,我使用此代码获取有关控件的信息:

public class CanvasAP : FrameworkElementAutomationPeer, IValueProvider
{
    public CanvasAP(Windows.UI.Xaml.Controls.Canvas owner) : base(owner)
    {
    }

    protected override AutomationControlType GetAutomationControlTypeCore()
    {
        return AutomationControlType.Custom;
    }

    protected override string GetClassNameCore()
    {
        return "Canvas";
    }

    protected override object GetPatternCore(PatternInterface patternInterface)
    {
        if (patternInterface == PatternInterface.Value)
        {
            return this;
        }

        return base.GetPattern(patternInterface);
    }


    #region Implementation of IValueProvider
    bool IValueProvider.IsReadOnly => true;

    string IValueProvider.Value
    {
        get
        {
            var str = "Test";
            var owner = (Windows.UI.Xaml.Controls.Canvas)Owner;

            foreach (var child in owner.Children)
            {
                str += $"{child.GetType()}";
            }

            return str;
        }
    }

    void IValueProvider.SetValue(string value)
    {
    }
    #endregion
}

在 UI 自动化客户端,我使用此代码获取有关控件的信息:

private static string GetElementString(UITestControl element, Int32 level = 0)
{
    var xaml = element as XamlControl;
    var str = "";
    for (var i = 0; i < level; i++)
        str += "  ";

    str += $"{element.ControlType} {element.ClassName} {element.Name} {xaml?.AutomationId ?? ""}\n";

    return str;
}

发现了我制造的错误 - 它是在调用 base.GetPattern 而不是 base.GetPatternCore 时调用的。

protected override object GetPatternCore(PatternInterface patternInterface)
{
    if (patternInterface == PatternInterface.Value)
        return this;

    return base.GetPatternCore(patternInterface);
}