我想在NUnit中编写系统测试,并使用ms的UI自动化调用UI。
由于某些原因,我的调用失败了 - 我在网上找到了一些提示,让我能够编写编译测试,但是我的断言失败了。
以下是一个可编译的最小示例。我的问题是示例中失败的测试。
应用程序XAML
由于某些原因,我的调用失败了 - 我在网上找到了一些提示,让我能够编写编译测试,但是我的断言失败了。
以下是一个可编译的最小示例。我的问题是示例中失败的测试。
应用程序XAML
<Application x:Class="InvokeTest.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:InvokeTest"
Startup="Application_Startup"/>
应用程序 CS
using System.Windows;
namespace InvokeTest
{
public partial class App : Application
{
private void Application_Startup(object sender, StartupEventArgs e)
{
var view = new MainWindow();
var viewmodel = new MainWindowViewModel();
view.DataContext = viewmodel;
view.Show();
}
}
}
Window XAML
<Window x:Class="InvokeTest.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:InvokeTest"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<TextBox x:Name="MyTextBox" x:FieldModifier="public" Text="{Binding TextProperty, UpdateSourceTrigger=PropertyChanged}" />
</Window>
窗口控制系统
using NUnit.Framework;
using System.Diagnostics;
using System.Threading;
using System.Windows;
using System.Windows.Automation.Peers;
using System.Windows.Automation.Provider;
using System.Windows.Controls;
namespace InvokeTest
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
}
public class MainWindowViewModel
{
string textfield;
public string TextProperty
{
get { DebugLog("getter"); return textfield; }
set { textfield = value; DebugLog("setter"); }
}
private void DebugLog(string function)
{
Debug.WriteLine(ToString() + " " + nameof(TextProperty) + " " + function + " was called. value: '" + textfield ?? "<null>" + "'");
}
[TestFixture, Apartment(ApartmentState.STA)]
public class WPFTest
{
MainWindow view;
MainWindowViewModel viewmodel;
[SetUp]
public void SetUp()
{
view = new MainWindow();
viewmodel = new MainWindowViewModel();
view.DataContext = viewmodel;
}
[Test]
public void SetTextBox_NoAutomation()
{
string expected = "I want to set this";
view.MyTextBox.Text = expected;
Assert.AreEqual(expected, viewmodel.TextProperty);
/*
Test Name: SetTextBox_NoAutomation
Test Outcome: Failed
Result Message:
Expected: "I want to set this"
But was: null
*/
}
[Test]
public void SetTextBox_UIAutomation()
{
string expected = "I want to set this";
SetValue(view.MyTextBox, expected);
Assert.AreEqual(expected, viewmodel.TextProperty);
/*
Test Name: SetTextBox_UIAutomation
Test Outcome: Failed
Result Message:
Expected: "I want to set this"
But was: null
*/
}
private static void SetValue(TextBox textbox, string value)
{
TextBoxAutomationPeer peer = new TextBoxAutomationPeer(textbox);
IValueProvider provider = peer.GetPattern(PatternInterface.Value) as IValueProvider;
provider.SetValue(value);
}
}
}
}
编辑 #1:@Nkosi 指出我的xaml中存在绑定失败的问题
编辑 #2:添加了一些模板以便进行手动测试,并添加了一个不使用UI自动化显示行为的用例。这只是一个附注,UI自动化才是本问题的核心。