Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WPF编码的UI测试不会触发绑定属性的更改_C#_Wpf_Testing_Coded Ui Tests - Fatal编程技术网

C# WPF编码的UI测试不会触发绑定属性的更改

C# WPF编码的UI测试不会触发绑定属性的更改,c#,wpf,testing,coded-ui-tests,C#,Wpf,Testing,Coded Ui Tests,我有一个WPF视图,并以编程方式在其上创建一个单选按钮: // public class MyView var radioButton = new RadioButton { Name = "NameGoesHere", Content = "Use alternative option", FontWeight = FontWeights.Bold, IsChecked = false }; radioButton.SetBinding(ToggleButton

我有一个WPF视图,并以编程方式在其上创建一个单选按钮:

// public class MyView
var radioButton = new RadioButton
{
    Name = "NameGoesHere",
    Content = "Use alternative option",
    FontWeight = FontWeights.Bold,
    IsChecked = false
};
radioButton.SetBinding(ToggleButton.IsCheckedProperty, new Binding("UseAlternativeOption")
{
    Converter = new CustomNullableBoolConverter(),
    ConverterParameter = true,
    Mode = BindingMode.TwoWay
});
panel.Children.Add(radioButton);
此单选按钮与我的视图模型中的属性具有双向绑定:

// public class MyViewModel
public bool? UseAlternativeOption
{
    get
    {
        return _useAlternativeOption;
    }
    set
    { // breakpoint here is not being hit when updated from automated UI test scenario
        _useAlternativeOption = value ?? false;
        OnPropertyChanged(nameof(UseAlternativeOption));
    }
}
现在,当我在应用程序中单击此单选按钮时,它会正确地调用属性设置器并设置值

问题是,当我尝试从编码的UI测试中执行相同操作时,它不起作用:

// public class MyUnitTests
var alternativeOptionRadioButton = UIControlBuilder<WpfRadioButton>.Builder()
    .Parent(ContentPane)
    .AutomationID(AlternativeOptionRadioButtonAutomationID)
    .Build();

alternativeOptionRadioButton.Selected = true;
Assert.AreEqual(true, viewModel.UseAlternativeOption);
我看到表单上的单选按钮被选中,并且可以看到selected属性为true,但是viewmodel setter没有被点击。我在_useAlternativeOption=value???上设置了一个断点??错误行,它不会被命中,而在UI测试场景之外运行并手动单击时会被命中

我有什么遗漏吗?
如何使RadioButton在自动化UI测试场景中触发其绑定的更改?

请尝试使用{SPACE}键执行SendKeys,而不是设置true/false值。如果空格不起作用,请尝试{ENTER}。我希望这能帮你渡过难关

if(!alternativeOptionRadioButton.Selected)
{
    Keyboard.SendKeys(alternativeOptionRadioButton, "{SPACE}");
}

编码的UI测试在与被测应用程序不同的进程中运行。因此,请确保调试器实际连接到正确的进程。除非还将调试器附加到该进程,否则不会在应用程序代码中遇到任何断点


要在Visual Studio 2017中执行此操作,请转到调试>附加到进程。。。然后选择要测试的应用程序的进程。

顺便说一句,在用户交互和自动UI测试场景中都会触发OnClick事件,因此肯定会单击单选按钮。您确定调试器已连接到正确的进程吗?运行编码UI测试的进程可能与实际应用程序进程不同。如果您只是尝试调试编码的UI测试,则不会在实际应用程序中遇到任何断点,除非您也附加到该进程。@DaveM这很有意义。实际上,模型正在正确更新,但我没有意识到我实际上是在调试UI测试,而不是正在测试的应用程序。谢谢您可以将其作为一个答案发布,这样它就可以帮助其他遇到这个问题的开发人员:谢谢您的回答。事实上,问题是我确实混淆了一个事实,即我没有调试我的应用程序,而编码的UI测试正在另一个进程中运行。