Reputation: 1031
I have a ui coded ui test in visual studio 2010. I want to write a code which will:
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?
Upvotes: 1
Views: 6129
Reputation: 1119
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.
Upvotes: 1
Reputation: 3
Check that the child has children before calling GetAllChildren(child, map)
if(child.HasChildren) { GetAllChildren(child, map); }
Upvotes: 0
Reputation: 942
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.
Upvotes: 0