Implementation of IValueProvider leads to UITestControlNotAvailableException

0

I'm struggling a bit with UI Automation for an WindowsStore app. I get an UITestControlNotAvailableException as soon as I implement IValueProvider. If I remove the implementation it works.

  • Microsoft.VisualStudio.TestTools.UITest.Extension.UITestControlNotAvailableException (The control is not available or not valid.)
  • InnerException: Points to a call to testControl.Name (Microsoft.VisualStudio.TestTools.UITesting.UITestControl.get_Name())
  • InnerException: An event was unable to invoke any of the subscribers (Exception from HRESULT: 0x80040201)

The StackTrace looks like this:

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 ...

On the app side I use this code to get information about the control:

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
}

On the UI Automation client side I use this code to get information about the control:

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;
}
xamarin
windows-store-apps
ui-automation
visualstudio.testtools
asked on Stack Overflow Feb 16, 2018 by Florian

1 Answer

0

Found the bug that I had made - it was in calling base.GetPattern instead of base.GetPatternCore.

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

    return base.GetPatternCore(patternInterface);
}
answered on Stack Overflow Feb 22, 2018 by Florian

User contributions licensed under CC BY-SA 3.0