[英]Coded UI discovering controls
我在 Visual Studio 2010 中有一個 ui 編碼的 ui 測試。我想編寫一個代碼,它將:
為了開始它,我寫了以下內容:
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);
}
}
但它插入到遞歸循環中並且不會結束它。
誰能幫我?
我認為為了避免可能的無限遞歸,您必須添加以下代碼:
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);
}
}
}
這樣您就可以避免多次考慮相同的 object 並遠離可能的視覺樹循環。
在 foreach 循環中調用 map.AddUIObject 和 GetAllChildren 之前,請檢查以確保 map 集合中不存在 object。
在調用 GetAllChildren(child, map) 之前檢查孩子是否有孩子
if(child.HasChildren) { GetAllChildren(child, map); }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.