【问题标题】:Coded UI discovering controls编码的 UI 发现控件
【发布时间】:2011-05-29 07:13:57
【问题描述】:

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

但它插入到递归循环中并没有结束它。

谁能帮帮我?

【问题讨论】:

  • 您是否添加了任何调试或工具来确定您在 foreach 循环的每次迭代中查看的控件?光看它,我不认为它会进入无限递归。

标签: c# coded-ui-tests


【解决方案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);    
      }
  }    
}

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 我怎样才能在所有子窗口中做到这一点?
    • 对不起,我只是想修复你的无休止的递归循环。我假设代码已经设置为查看所有子窗口。
    【解决方案3】:

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

    如果(孩子.HasChildren){ GetAllChildren(孩子,地图); }

    【讨论】:

    • 子窗口呢?
    • 子窗口成为父窗口,因为这是递归的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-05
    • 1970-01-01
    • 2017-04-13
    相关资源
    最近更新 更多