1

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

  1. 发现窗口和子窗口上的所有控件,即按钮、网格、标签
  2. 编写一个 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);    
    }    
}

但它插入到递归循环中并且不会结束它。

谁能帮我?

4

3 回答 3

1

我认为为了避免可能的无限递归,您必须添加以下代码:

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

这样您就可以避免多次考虑同一个对象并远离可能的视觉树循环。

于 2011-06-08T07:50:11.433 回答
0

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

于 2011-06-01T12:15:23.663 回答
0

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

如果(孩子.HasChildren){
   GetAllChildren(孩子,地图);
}
于 2011-06-02T19:44:02.223 回答