2014-01-27 87 views
6

我創建了一個小型WPF應用程序(只是爲了探討編碼UI測試的工作原理)。我的應用程序包含ItemsControl,但編碼的UI測試生成器UIMap找不到合適的控件。如何讓Microsoft Coded UI測試生成器識別ItemsControl?

XAML

<ItemsControl ItemsSource="{Binding Path=Customers, Mode=OneTime}" 
       AutomationProperties.AutomationId="CustomersItemsControl"> 
    <ItemsControl.ItemTemplate> 
     <DataTemplate> 
      <TextBlock AutomationProperties.AutomationId="{Binding Path=Id, StringFormat='Customer_{0}', Mode=OneWay}" 
         Margin="5"> 
       <TextBlock.Text> 
        <MultiBinding StringFormat="{}{1}, {0}"> 
         <Binding Path="FirstName" /> 
         <Binding Path="LastName" /> 
        </MultiBinding> 
       </TextBlock.Text> 
      </TextBlock> 
     </DataTemplate> 
    </ItemsControl.ItemTemplate> 
</ItemsControl> 

,我有是,即使WPF有ItemsControlAutomationPeer,編碼的UI測試生成器無法找到要添加到的UIMap控制的問題。我解決這個問題的唯一方法是獲取Root AutomationElement,然後利用TreeWalker.ControlViewWalker(我使用this answer中的代碼),但是該解決方案不允許我訪問控制本身,只有AutomationElement。理想情況下,因爲控制器有一個AutomationPeer,所以我想知道是否有更好的方法來實現這種控制(即使不可能達到UIMap)。


另外,附註。我已經瞭解Visual Studio在執行Coded UI測試時會忽略TextBlock,因爲可能會產生如此多的內容。現在我更關注的是ItemsControl本身,而不是從其中生成的個人TextBlock

回答

4

經過深入研究,我發現了一個半解決方案。我看着UIMap設計文件如何生成的代碼爲ListView(我調整一下代碼,爲我自己的變量):

var itemscontrol = new WpfList(window); 
itemscontrol.SearchProperties[WpfList.PropertyNames.AutomationId] = "CustomersItemsControl"; 
itemscontrol.WindowTitles.Add("MainWindow"); 

var count = itemscontrol.Items.Count(); // Returns the correct value! 

我複製此代碼,它似乎爲ItemsControl工作爲好,至少有一些屬性,如WpfList.Items。所以,這是我猜想的部分解決方案。

相關問題