简体   繁体   中英

Setting `VirtualizingPanel.IsVirtualizing` with TestStack.White

When testing virtualized panels I need to set the VirtualizingPanel.IsVirtualizing Property so that Teststack.White can interact with them like with non virtualized panels.

This helps me especially when panels have a lot of content.

I do not want to set VirtualizingPanel.IsVirtualizing statically so I do not have to deliver it like that to my customers.

To play around with a minimal example you will need a window.

<Window x:Class="DataGridTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:DataGridTest"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <DataGrid
        AutomationProperties.AutomationId="MyDataGRID"
        ItemsSource="{Binding MyItems}" 
        VirtualizingPanel.IsVirtualizing="True" >
        <!-->  
        "IsVirtualizing Defaults to True."
        "Setting this to False makes the test pass but is a poor choice for production code."
        "Somehow I need to be able to change this programatically during testing."
        </!-->
    </DataGrid>
</Window>

Code behind for the window above.

using System.Collections.Generic;
using System.Windows;

namespace DataGridTest
{
    public class Item
    {
        private string str;
        public Item(string str) { this.str = str; }
        public string Value { get { return str; } }
        public int Length { get { return str.Length; } }
        public int Hash { get { return str.GetHashCode(); } }
    }

    public partial class MainWindow : Window
    {
        List<Item> myitems;
        public List<Item> MyItems { get { return myitems; } }

        public MainWindow()
        {
            InitializeComponent();

            myitems = new List<Item>();
            for (int i = 0; i < 800; ++i)
            {
                myitems.Add(new Item($"Item {i}"));
            }

            DataContext = this;
        }
    }
}

And finally a Testing project:

using NUnit.Framework;
using System.Diagnostics;
using TestStack.White;
using TestStack.White.UIItems;
using TestStack.White.UIItems.WindowItems;

namespace NunitTest
{
    [TestFixture]
    public class Class1
    {
        private Application app;
        private Window window;

        [OneTimeSetUp]
        public void OneTimeSetUp()
        {
            ProcessStartInfo info = new ProcessStartInfo( $"{TestContext.CurrentContext.WorkDirectory}/DataGridTest.exe");
            info.WorkingDirectory = TestContext.CurrentContext.WorkDirectory;
            app = Application.Launch(info);
            window = app.GetWindow("MainWindow");
        }

        [OneTimeTearDown]
        public void OneTimeTearDown()
        {
            window.Close(); window = null;
            app.Close(); app = null;
        }

        [Test]
        public void test()
        {
            ListView list = window.Get<ListView>("MyDataGRID");
            SetIsVirtualizing(list, false);
            Assert.AreEqual(800, list.Rows.Count, "This fails for virtualized panels");
            SetIsVirtualizing(list, true);
        }

        private void SetIsVirtualizing(ListView list, bool value)
        {
            //insert magic - I tried a couple of things but I just can not set this dependency property
        }
    }
}

Please help be to understand how VirtualizingPanel.IsVirtualizing can be set during testing.

I had some success with adding a collapsed textbox to interact with the datacontext. Although I am very unhappy about that solution it does pass the testing.

Here are modifications to the code that I made:

window

<StackPanel>
    <TextBox 
    AutomationProperties.AutomationId="MyItems_IsVirtualizing_Injector"
    Text="{Binding MyItems_IsVirtualizing_Injector}" Visibility="Collapsed"/>
    <DataGrid
    AutomationProperties.AutomationId="MyDataGRID"
    ItemsSource="{Binding MyItems}" 
    VirtualizingPanel.IsVirtualizing ="{Binding MyItems_IsVirtualizing}"
        >
        <!-->  
    "IsVirtualizing Defaults to True."
    "Setting this to False makes the test pass but is a poor choice for production code."
    "Somehow I need to be able to change this programatically during testing."
    </!-->
    </DataGrid>
</StackPanel>

code behind

    string injector = true.ToString();
    public event PropertyChangedEventHandler PropertyChanged = delegate { };
    public string MyItems_IsVirtualizing_Injector
    {
        get { return injector; }
        set
        {
            injector = value;
            PropertyChanged(this, new PropertyChangedEventArgs("MyItems_IsVirtualizing_Injector"));
            PropertyChanged(this, new PropertyChangedEventArgs("MyItems_IsVirtualizing"));
        }
    }
    public bool MyItems_IsVirtualizing { get { return string.Equals(true.ToString(), MyItems_IsVirtualizing_Injector); } }

testing

    private void SetIsVirtualizing(ListView list, bool value)
    {
        var injector = window.Get<TextBox>("MyItems_IsVirtualizing_Injector");
        injector.Text = value.ToString();
    }

EDIT: Since my actual usecase is counting the elements I actually settled for another solution that can be called using CountElements(list.AutomationElement)

    private static int CountElements(AutomationElement container)
    {
        var patterns = container.GetSupportedPatterns();
        var itemContainer = container.GetCurrentPattern(ItemContainerPattern.Pattern) as ItemContainerPattern;

        List<object> elements = new List<object>();
        var element = itemContainer.FindItemByProperty(null, null, null);
        while (element != null)
        {
            elements.Add(element);
            element = itemContainer.FindItemByProperty(element, null, null);
        }
        return elements.Count;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM