2016-11-25 2 views
2

したがって、私はUI WPFアプリケーションをテストしようとしています。私はテストのためのTestStack.Whiteフレームワークを使用しています。 UIにはカスタムコントロールDragDropItemsControlがあります。このコントロールはItemsControlから継承されます。では、どうすればこのコントロールをテストできますか?TestStack.White.UIItemsでItemsControlをテストする方法

<wpf:DragDropItemsControl x:Name="uiTabsMinimizedList" 
             Margin="0 0 0 5" 
             VerticalAlignment="Top" 
             AllowDropOnItem="False" 
             DragDropTemplate="{StaticResource TemplateForDrag}" 
             ItemDropped="uiTabsMinimizedList_ItemDropped" 
             ItemsSource="{Binding ElementName=uiMain, 
                  Path=MinimizedTabs}" 
             ScrollViewer.HorizontalScrollBarVisibility="Disabled" 
             ScrollViewer.VerticalScrollBarVisibility="Disabled" 
             TextBlock.Foreground="{Binding RelativeSource={RelativeSource Mode=FindAncestor, 
                            AncestorType=UserControl}, 
                    Path=Foreground}"> 
       <wpf:DragDropItemsControl.ItemTemplate> 
        <DataTemplate> 
         <Border > 
          <TextBlock Cursor="Hand" Text="{Binding Panel.Label}" /> 
         </Border> 
        </DataTemplate> 
       </wpf:DragDropItemsControl.ItemTemplate> 
      </wpf:DragDropItemsControl> 

テストできますか?

+0

セットの各アイテムに個々の名前/ AutomationIdを与える方法を尋ねていますか? – LordWilmore

+0

@LordWilmoreはい。私はItemsControlから各項目を取得する解決策が見つかりませんでした –

+0

したがって、何か一意にautomationproperties.automationidを設定する必要があります。したがって、適切な文字列を選択し、オブジェクト内で一意であるものにバインドするプレフィックスを追加します(例: ID。 – LordWilmore

答えて

0

DragDropItemsControlの独自のAutomationPeerを作成し、独自のコントロールアイテムを作成する必要がある場合、AutomationIdをアイテムオブジェクトの識別子として定義することができます。

public class DragDropItemsControl : ItemsControl 
{ 
    protected override AutomationPeer OnCreateAutomationPeer() 
    { 
     return new DragDropItemsAutomationPeer(this); 
    } 
} 

カスタムAutomationPeerクラスです。

public class DragDropItemsControlAutomationPeer : ItemsControlAutomationPeer 
{ 
    public DragDropItemsControlAutomationPeer(DragDropItemsControl owner) 
     : base(owner) 
    { 
    } 

    protected override string GetClassNameCore() 
    { 
     return "DragDropItemsControl"; 
    } 

    protected override ItemAutomationPeer CreateItemAutomationPeer(object item) 
    { 
     return new DragDropItemsControlItemAutomationPeer(item, this); 
    } 
} 

コントロールアイテムのカスタムAutomationPeerクラスです。 ここで重要な部分は、メソッドGetAutomationIdCore()の実装です。

public MyMainWindow() 
{ 
    InitializeComponent(); 

    List<MyTestItemObject> items = new List<MyTestItemObject>(); 
    items.Add(new MyTestItemObject() { Title = "Learning TestStack.White", ItemId="007" }); 
    items.Add(new MyTestItemObject() { Title = "Improve my english", ItemId = "008" }); 
    items.Add(new MyTestItemObject() { Title = "Work it out", ItemId = "009" }); 

    icTodoList.ItemsSource = items; 
} 
public class MyTestItemObject 
{ 
    public string Title { get; set; } 
    public string ItemId { get; set; } 
} 

の背後にあるコードで、次のXAMLコード

<local:MyItemsControl x:Name="icTodoList" AutomationProperties.AutomationId="TestItemsControl"> 
    <local:MyItemsControl.ItemTemplate> 
     <DataTemplate> 
      <Border > 
       <TextBlock Cursor="Hand" Text="{Binding Title}" /> 
      </Border> 
     </DataTemplate> 
    </local:MyItemsControl.ItemTemplate> 
</local:MyItemsControl> 

初期化のために

public class DragDropItemsControlItemAutomationPeer : ItemAutomationPeer 
{ 
    public DragDropItemsControlItemAutomationPeer(object item, ItemsControlAutomationPeer itemsControlAutomationPeer) 
     : base(item, itemsControlAutomationPeer) 
    { 
    } 

    protected override string GetClassNameCore() 
    { 
     return "DragDropItemsControl_Item"; 
    } 

    protected override string GetAutomationIdCore() 
    { 
     return (base.Item as MyTestItemObject)?.ItemId; 
    } 

    protected override AutomationControlType GetAutomationControlTypeCore() 
    { 
     return base.GetAutomationControlType(); 
    } 
} 

我々は

UIAVerify screen

サンプルUIAVerifyで見ることができます値を確認するコード

// retrieve the custom control 
IUIItem theItemsControl = window.Get(SearchCriteria.ByAutomationId("008")); 

if (theItemsControl is CustomUIItem) 
{ 
    // retrieve the custom control container 
    IUIItemContainer controlContainer = (theItemsControl as CustomUIItem).AsContainer(); 

    // get the child components 
    WPFLabel theTextBlock = controlContainer.Get<WPFLabel>(SearchCriteria.Indexed(0)); 

    // get the text value 
    string textValue = theTextBlock.Text; 
} 
関連する問題