AutomationPeer.GetChildrenCore()只报先child到VisualStudio.TestTools

AutomationPeer.GetChildrenCore () only reports first child to VisualStudio.TestTools

我无法正确覆盖 GetChildrenCore。我将其用于 Canvas 以获取有关它的信息 children(直线,矩形)。

输出正确地指出了第一个 child 但错过了第二个。即使 Canvas 已经包含两者。

Custom Canvas  
    Custom Line Childs of Canvas parent: 2  

应该是这样的:

Custom Canvas  
    Custom Line Childs of Canvas parent: 2  
    Custom Rectangle Childs of Canvas parent: 2  

App端端:

public class ElementAP : FrameworkElementAutomationPeer
{
    private FrameworkElement Owner = null;
    private Int32 Count = 0;

    public ElementAP(FrameworkElement owner, Int32 count) : base (owner)
    {
        Owner = owner;
        Count = count;
    }

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

    protected override string GetClassNameCore()
    {
        return $"{Owner.GetType().Name} Childs of Canvas parent: {Count}"; 
    }
}

public class CanvasAP : FrameworkElementAutomationPeer
{
    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 IList<AutomationPeer> GetChildrenCore()
    {
        var owner = (Windows.UI.Xaml.Controls.Canvas)Owner;           
        var list = new List<AutomationPeer> ();

        foreach (var child in owner.Children)
        {
            var peer = new ElementAP(child as FrameworkElement, owner.Children.Count);

            list.Add(peer);
        }

        return list;
    }
}       

UI 测试方:

private static string WalkTree(UITestControl element, Int32 level = 0)
{
    var children = element.GetChildren();

    var str = "";
    foreach (var c in children)
    {
        str += GetElementString(c, level);
        str += WalkTree(c, level + 1);
    }

    return str;
}

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

我终于找到了答案。当为 children`s AutomationPeers 使用缓存时,它工作得很好。

public class ElementAP : FrameworkElementAutomationPeer
{
    public UIElement Element { get { return Owner; } }

    public ElementAP(FrameworkElement owner) : base(owner)
    {
    }

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

    protected override string GetClassNameCore()
    {
        return Owner.GetType().Name;
    }
}

public class CanvasAP : FrameworkElementAutomationPeer
{
    private List<ElementAP> _cachedAutomationPeers = new List<ElementAP>();

    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 IList<AutomationPeer> GetChildrenCore()
    {
        var owner = (Windows.UI.Xaml.Controls.Canvas)Owner;

        if (owner.Children.All(c => c is CanvasA))
            return base.GetChildrenCore();

        var list = new List<ElementAP>();

        foreach (var child in owner.Children)
        {
            var peer = _cachedAutomationPeers.FirstOrDefault(p => p.Element == child) ?? new ElementAP(child as FrameworkElement);

            list.Add(peer);
        }

        _cachedAutomationPeers = list;

        return list.Cast<AutomationPeer>().ToList();
    }
}