2011-05-29 17 views
1

我在Visual Studio中的UI編碼的UI測試2010 我想寫一個代碼將:編碼的UI控件發現

  1. 來到一個窗口上的控件和子窗口這是按鈕,grid,label
  2. 用代碼中的控件名稱編寫一個uimap。

對於啓動它,我已經寫了以下內容:

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

但它插入到遞歸循環,不會結束。

任何人都可以幫助我嗎?

+1

您是否添加了任何調試或工具來確定您在foreach循環的每次迭代中查看哪些控件?只是從它看,我不會指望它進入無限遞歸。 – 2011-06-01 12:14:02

回答

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

這樣你避免多次考慮同一個對象,並遠離可能的視覺樹循環。

0

在調用foreach循環中的map.AddUIObject和GetAllChildren之前,請檢查以確保對象不存在於地圖集合中。

+0

以及如何在所有子窗口中執行此操作? – gln 2011-06-02 05:30:39

+0

對不起,我只是想解決你無休止的遞歸循環。我假設代碼已經設置爲查看所有子窗口。 – 2011-06-02 13:08:02

0

檢查孩子叫GetAllChildren(兒童,地圖)

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

和孩子的窗戶怎麼樣? – gln 2011-06-05 09:41:09

+0

子窗口因爲遞歸而成爲父窗口。 – BrandX 2011-10-20 20:38:45