无法在展开的行中定位控件



类似于这个问题,我试图开发一个编码的UI测试,它需要对位于WPF行(行细节)中的一些控件执行一些断言。不幸的是,编码UI检查器似乎无法在展开的行中找到任何控件,因为它将这些控件标识为自定义控件(UI . datagriddetailspresenter)的子控件,这是该行的子控件。

这个网格的应用程序的xaml非常简单,如下所示:

<DataGrid.RowDetailsTemplate>
    <DataTemplate>
        <TextBlock Text="{Binding Details}" Margin="10" />
    </DataTemplate>
</DataGrid.RowDetailsTemplate>

有没有人设法访问行细节使用在一个编码的UI测试之前?

我有同样的问题,编码UI检查器看到Uia。DataGridDetailsPresenter和它的孩子,但不能与他们一起工作在测试运行

好吧,你的应用程序可能有问题,这个DataGrid不是为编码ui自动化发布的。我对TabPage内容也有同样的问题。

如果你想知道,编码UI在Wpf应用程序中看到什么执行这段代码,你会得到一些元素映射在输出-复制到excel。如果这找到了您需要的控件,那么您的测试也应该找到它们。

    /*
     *  Method used for get map of element and it's children
     *  element - element whose children you want to explore
     *  comma - use empty string - ""
    */
    public void getMap(WpfControl element, String comma)
    {
        comma = comma + "t";
        foreach (Object child in element.GetChildren())
        {
            WpfControl childAsWpf = child as WpfControl;
            if (childAsWpf != null && !(element is WpfListItem))
            {
                logElementInfo(childAsWpf, comma);
                getMap(childAsWpf, comma);
            }
            else
            {
                System.Diagnostics.Debug.WriteLine("--------- object: {0}; type: {1}", child, child.GetType());
            }
        }
    }
    // logs element info in Output
    private void logElementInfo(WpfControl element, String comma)
    {
        System.Diagnostics.Debug.WriteLine("{0}AutomationId: {1}, ClassName: {2}, ControlType: Wpf{3}", comma, element.AutomationId, element.ClassName, element.ControlType);
    }

我最终通过编写自己的DataGrid控件来解决这个问题,该控件公开了期望的自动化对等体。

通用自动化节点

class GenericAutomationPeer : UIElementAutomationPeer
{
    public GenericAutomationPeer(UIElement owner)
        : base(owner)
    {
    }
    protected override List<AutomationPeer> GetChildrenCore()
    {
        List<AutomationPeer> list = base.GetChildrenCore();
        list.AddRange(GetChildPeers(Owner));
        return list;
    }
    private List<AutomationPeer> GetChildPeers(UIElement element)
    {
        List<AutomationPeer> automationPeerList = new List<AutomationPeer>();
        for (int i = 0; i < VisualTreeHelper.GetChildrenCount(element); i++)
        {
            UIElement child = VisualTreeHelper.GetChild(element, i) as UIElement;
            if (child != null)
            {
                AutomationPeer childPeer = UIElementAutomationPeer.CreatePeerForElement(child);
                if (childPeer != null)
                {
                    automationPeerList.Add(childPeer);
                }
                else
                {
                    automationPeerList.AddRange(GetChildPeers(child));
                }
            }
        }
        return automationPeerList;
    }
}

自定义数据表格

public class CustomDataGrid : DataGrid
{
    //Override automation peer that the base class provides as it doesn't expose automation peers for the row details
    protected override AutomationPeer OnCreateAutomationPeer()
    {
        return new GenericAutomationPeer(this);
    }
}

最新更新