- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
在测试虚拟化面板时,我需要设置 VirtualizingPanel.IsVirtualizing
属性,以便 Teststack.White
可以像与非虚拟化面板一样与它们交互。
这对我很有帮助,尤其是当面板包含大量内容时。
我不想静态设置 VirtualizingPanel.IsVirtualizing
,这样我就不必像那样将它交付给我的客户。
要玩一个最小的例子,你需要一个窗口。
<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>
上面窗口的代码隐藏。
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;
}
}
}
最后是一个测试项目:
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
}
}
}
请帮助理解如何在测试期间设置VirtualizingPanel.IsVirtualizing
。
最佳答案
我在添加折叠文本框以与数据上下文交互方面取得了一些成功。虽然我对该解决方案非常不满意,但它确实通过了测试。
以下是我对代码所做的修改:
窗口
<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>
代码隐藏
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); } }
测试
private void SetIsVirtualizing(ListView list, bool value)
{
var injector = window.Get<TextBox>("MyItems_IsVirtualizing_Injector");
injector.Text = value.ToString();
}
编辑:因为我的实际用例正在计算元素,所以我实际上选择了另一个可以使用 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;
}
关于c# - 使用 TestStack.White 设置 `VirtualizingPanel.IsVirtualizing`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42535434/
什么情况下返回值为true? 谢谢大家! public class Test1 { public static void main(String[] args) { try {
本文整理了Java中org.apache.catalina.WebResource.isVirtual()方法的一些代码示例,展示了WebResource.isVirtual()的具体用法。这些代码示
在测试虚拟化面板时,我需要设置 VirtualizingPanel.IsVirtualizing 属性,以便 Teststack.White 可以像与非虚拟化面板一样与它们交互。 这对我很有帮助,尤其
我做了一个简单的例子来重现我在 TreeView 上遇到的问题.如果我在 TreeView 中选择一个项目,向下滚动,更改 Focus ,然后在 TreeView 中选择一个项目再次,列表开始跳来跳去
几乎没有关于设置影响的信息; VirtualizingStackPanel.IsVirtualizing="True" 和 EnableRowVirtualization="True" EnableC
我在使用 this question 时遇到了类似的问题但是 VirtualizingStackPanel.IsVirtualizing="False" 没有解决我的问题。有没有人面临同样的问题? 问
我是一名优秀的程序员,十分优秀!