1
votes

I have a ui coded ui test in visual studio 2010. I want to write a code which will:

  1. Discover all the controls on a window and child windows which are button, grid, label
  2. write a uimap with the id which is the name of the control in the code.

For starting it, I've write the following:

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

But it insert into the recursive loop and doesn't end it.

Can anyone help me?

3
Have you added any debugging or instrumentation to determine what control you are looking at in each iteration of the foreach loop? Just from looking at it I wouldn't expect it to enter into a infinite recursion.Jeff Machamer

3 Answers

1
votes

I think that to avoid possible infinite recursion you have to add this code:

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

This way you avoid to consider the same object multiple times and keep away from possible visual tree cycle.

0
votes

Before you call map.AddUIObject and GetAllChildren in the foreach loop, check to make sure the object doesn't already exist in the map collection.

0
votes

Check that the child has children before calling GetAllChildren(child, map)

if(child.HasChildren) {
   GetAllChildren(child, map);
}