gpt4 book ai didi

wpf - 如何使用 TestStack.White.UIItems 测试 ItemsControl

转载 作者:行者123 更新时间:2023-12-02 04:37:39 25 4
gpt4 key购买 nike

所以我正在尝试测试 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>

我们可以测试吗?

最佳答案

您必须为您的 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中看到

UIAVerify screen

检查值的示例代码

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

关于wpf - 如何使用 TestStack.White.UIItems 测试 ItemsControl,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40801528/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com