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

3

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

我们能够进行测试吗?


你是在问如何为集合中的每个项分配独立的名称/自动化ID吗? - LordWilmore
@LordWilmore 是的,我没有找到任何解决方法从ItemsControl获取每个项。 - Qutfullo Ochilov
所以你需要将automationproperties.automationid设置为唯一的值。选择一个合适的字符串,并添加一个前缀,绑定到对象内部的唯一标识符,例如ID。 - LordWilmore
1个回答

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接