【问题标题】:Searching for tree item when I don't know exact tree hierarchy当我不知道确切的树层次结构时搜索树项
【发布时间】:2017-07-10 14:03:10
【问题描述】:

我正在尝试在编码的 UI 测试中选择一个树项目,但我不知道整个层次结构。

例子:

  • mssql 连接
    • 表
      • 未知
        • 名字

有没有办法在不指定整个路径的情况下搜索此 FirstName 树项并指定它的深度如此之多?

看起来任何搜索配置属性都不会这样做。

【问题讨论】:

  • 理论上智能搜索会找到它,但在更大的树中它可能会首先超时(或者如果未知数完全不同,它可能会忽略它)。假设您知道它总是深入 X 控件,则有一个 MaxDepth 搜索属性可能有助于减少错误搜索路径的数量。

标签: c# ui-automation coded-ui-tests


【解决方案1】:

如果 FirstName 在树中是唯一的,那么您可以使用 PInvoke,并且不需要指定深度:

    public static List<IntPtr> GetChildWindows(IntPtr parent)
    {
        var result = new List<IntPtr>();
        var listHandle = GCHandle.Alloc(result);
        try
        {
            var childProc = new User32.EnumWindowsProc(EnumWindow);
            User32.EnumChildWindows(parent, childProc, GCHandle.ToIntPtr(listHandle));
        }
        finally
        {
            if (listHandle.IsAllocated)
                listHandle.Free();
        }
        return result;
    }

    private static bool EnumWindow(IntPtr handle, IntPtr pointer)
    {
        var gch = GCHandle.FromIntPtr(pointer);
        var list = gch.Target as List<IntPtr>;
        if (list == null)
        {
            throw new InvalidCastException("GCHandle Target could not be cast as List<IntPtr>");
        }
        list.Add(handle);
        // Modify this to check to see if you want to cancel the operation, then return a null here
        return true;
    }

    public delegate bool EnumWindowsProc(IntPtr hWnd, IntPtr lParam);

    [DllImport("user32.dll")]
    [return: MarshalAs(UnmanagedType.Bool)]
    public static extern bool EnumChildWindows(IntPtr window, EnumWindowsProc callback, IntPtr i);

    // sample usage:
    public void findWindowUser32()
    {
        foreach (IntPtr child in GetChildWindows(User32.FindWindow(null, "Untitled - Notepad")))
        {
            StringBuilder sb = new StringBuilder(100);
            User32.GetClassName(child, sb, sb.Capacity);

            if (sb.ToString() == "Edit")
            {
                uint wparam = 0 << 29 | 0;
                User32.PostMessage(child, WindowsConstants.WM_KEYDOWN, (IntPtr)Keys.H, (IntPtr)wparam);
            }
        }
    }

【讨论】:

  • +1 换一种方式来看待问题。与使用内置测试框架相比,使用 PInvoke 的速度如何?
  • 有趣的是你应该问。性能证明是我喜欢这个解决方案的原因之一。性能要好得多,这很可能是因为 GetChildWindows 调用只收集了窗口句柄。获取名称 (GetClassName) 的额外调用也不会增加太多开销。使用 UIAutomation 类并在 TreeScope 设置为 Descendants 的情况下调用 GetChildren 也可以很好地工作,但会收集更多数据并且速度要慢得多。
  • 注意:我认为 CodedUI 使用 MSAA、UIAutomation 类,可能还有更多技术。
【解决方案2】:

当您的控件被映射到 UI 地图中时,可能会使用完整的层次结构,例如

mssql 连接 -表 --未知1 ---名字

产生 4 个映射控件。

您可以手动编辑 uimap .xml 文件,小心删除 -Unknown1 元素,并确保 MatchExactHierarchy 已关闭。 这样一来搜索最初会失败,继续使用启发式方法在树中查找比直接子级更深的元素,并且应该找到您的控件。

【讨论】:

    【解决方案3】:
      public static UITestControl GetTreeItem(UITestControl TreeControl, string ItemName, bool ContainsTrue = true)
        {
            AutomationElement tree = AutomationElement.FromHandle(TreeControl.WindowHandle);
            System.Windows.Automation.ControlType controlType = tree.Current.ControlType;
            //Get collection of tree nodes.
            AutomationElementCollection treeNodeCollection = null;
            treeNodeCollection = tree.FindAll(TreeScope.Descendants,
                    new System.Windows.Automation.PropertyCondition(AutomationElement.ControlTypeProperty,
                            System.Windows.Automation.ControlType.TreeItem));
            UITestControl ReqTreeItem = new UITestControl();
            foreach (AutomationElement item in treeNodeCollection)
            {
                if ((item.Current.Name == ItemName) && (!ContainsTrue))
                {
                    ReqTreeItem = UITestControlFactory.FromNativeElement(item, "UIA");
                    break;
                }
                if ((item.Current.Name.Contains(ItemName)) && (ContainsTrue))
                {
                    ReqTreeItem = UITestControlFactory.FromNativeElement(item, "UIA");
                    break;
                }
            }
            return ReqTreeItem;
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-04-09
      • 2017-04-18
      • 1970-01-01
      • 1970-01-01
      • 2013-01-31
      • 2018-01-11
      • 1970-01-01
      相关资源
      最近更新 更多