当我不知道确切的树层次结构时搜索树项

时间:2012-07-16 21:46:32

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

我正在尝试在Coded UI Test中选择一个树项,但我不知道整个层次结构。

示例:

  
      
  • mssql连接   
        
    • 表   
          
      • 未知   
            
        •   
      •   
    •   
  •   

有没有办法搜索这个FirstName树项,并指定它是如此多的深度,而不指定整个路径?

看起来没有任何搜索配置属性可以执行此操作。

3 个答案:

答案 0 :(得分: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 :(得分:0)

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

mssql连接 -tables --Unknown1 ---姓

产生了4个映射控件。

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

答案 2 :(得分:0)

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