如何使用 TestStack.White.UIItems 测试 ItemsControl
Posted
技术标签:
【中文标题】如何使用 TestStack.White.UIItems 测试 ItemsControl【英文标题】:How to test ItemsControl with TestStack.White.UIItems 【发布时间】:2016-11-25 09:26:24 【问题描述】:所以我正在尝试测试 UI WPF 应用程序。我正在使用 TestStack.White 框架进行测试。 UI 有自定义控件 DragDropItemsControl。此控件继承自 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>
我们可以测试吗?
【问题讨论】:
您是在问如何为集合中的每个项目指定一个单独的名称/AutomationId? @LordWilmore 是的。我没有找到任何解决方案从 ItemsControl 获取每个项目 因此您需要将automationproperties.automationid 设置为独特的。所以选择一个合适的字符串,并添加一个前缀来绑定到对象中唯一的东西,例如一个 ID。 【参考方案1】:您必须为您的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 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();
对于下面的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 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;
我们可以在 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;
【讨论】:
以上是关于如何使用 TestStack.White.UIItems 测试 ItemsControl的主要内容,如果未能解决你的问题,请参考以下文章
如何在自动布局中使用约束标识符以及如何使用标识符更改约束? [迅速]
如何使用 AngularJS 的 ng-model 创建一个数组以及如何使用 jquery 提交?