C# 编码UI发现控件

C# 编码UI发现控件,c#,coded-ui-tests,C#,Coded Ui Tests,我在VisualStudio2010中有一个ui编码的ui测试。 我想写一段代码,它将: 查找窗口和子窗口上的所有控件,即按钮、网格和标签 编写一个uimap,id是代码中控件的名称 为了启动它,我写了以下内容: public void CodedUITestMethod1() { string uiTestFileName = @"D:\dev11\ConsoleApplication1\TestProject1\UIMap.uitest"; UITest uiTest

我在VisualStudio2010中有一个ui编码的ui测试。 我想写一段代码,它将:

  • 查找窗口和子窗口上的所有控件,即按钮、网格和标签
  • 编写一个uimap,id是代码中控件的名称
  • 为了启动它,我写了以下内容:

    public void CodedUITestMethod1()
    {    
       string uiTestFileName = @"D:\dev11\ConsoleApplication1\TestProject1\UIMap.uitest";
    
       UITest uiTest = UITest.Create(uiTestFileName);
    
       Microsoft.VisualStudio.TestTools.UITest.Common.UIMap.UIMap newMap = new Microsoft.VisualStudio.TestTools.UITest.Common.UIMap.UIMap(); 
       newMap.Id = "UIMap"; 
       uiTest.Maps.Add(newMap);
    
       GetAllChildren(BrowserWindow.Launch(new Uri("http://bing.com")), uiTest.Maps[0];);
       uiTest.Save(uiTestFileName);    
    }
    
    private void GetAllChildren(UITestControl uiTestControl, Microsoft.VisualStudio.TestTools.UITest.Common.UIMap.UIMap map)
    {
       foreach (UITestControl child in uiTestControl.GetChildren())
       {
           map.AddUIObject((IUITechnologyElement)child.GetProperty(UITestControl.PropertyNames.UITechnologyElement));
    
           GetAllChildren(child, map);    
        }    
    }
    
    但是它插入到递归循环中,并没有结束它


    有人能帮我吗?

    在调用foreach循环中的map.AddUIObject和GetAllChildren之前,请检查以确保该对象在地图集合中不存在

    在foreach循环中调用map.AddUIObject和GetAllChildren之前,请检查以确保该对象在映射集合中不存在

    在调用GetAllChildren(child,map)之前,请检查子级是否有子级

    if(child.HasChildren){ GetAllChildren(child,map); }
    在调用GetAllChildren(child,map)之前,请检查该子级是否具有子级

    if(child.HasChildren){ GetAllChildren(child,map); }
    我认为为了避免可能的无限递归,您必须添加以下代码:

    private void GetAllChildren(UITestControl uiTestControl, Microsoft.VisualStudio.TestTools.UITest.Common.UIMap.UIMap map)
    {
      foreach (UITestControl child in uiTestControl.GetChildren())
      {
          IUITechnologyElement tElem=(IUITechnologyElement)child.GetProperty(UITestControl.PropertyNames.UITechnologyElement);
          if (!map.Contains(tElem))
          {
              map.AddUIObject(tElem);
              GetAllChildren(child, map);    
          }
      }    
    }
    

    这样,避免多次考虑同一对象,避免可能的视觉树循环。

    < P>我认为,为了避免可能的无限递归,必须添加此代码:

    private void GetAllChildren(UITestControl uiTestControl, Microsoft.VisualStudio.TestTools.UITest.Common.UIMap.UIMap map)
    {
      foreach (UITestControl child in uiTestControl.GetChildren())
      {
          IUITechnologyElement tElem=(IUITechnologyElement)child.GetProperty(UITestControl.PropertyNames.UITechnologyElement);
          if (!map.Contains(tElem))
          {
              map.AddUIObject(tElem);
              GetAllChildren(child, map);    
          }
      }    
    }
    

    这样,您避免多次考虑同一对象,并且远离可能的视觉树循环。< /P>是否添加了任何调试或检测工具来确定在Frach循环的每次迭代中您所看到的控件?仅仅从它来看,我不希望它进入无限递归。您是否添加了任何调试或工具来确定在foreach循环的每个迭代中要查看的控件?仅仅从它来看,我不希望它进入无限递归。我怎么能在所有子窗口中这样做呢?对不起,我只是想修复你的无休止的递归循环。我假设代码已设置为查看所有子窗口。我如何在所有子窗口中执行此操作?抱歉,我只是想修复您的无休止的递归循环。我假设代码已经设置为查看所有子窗口。那么子窗口呢?子窗口成为父窗口是因为这是递归的。而子窗口呢?子窗口成为父窗口是因为这是递归的。