mercredi 20 février 2019

WPF coded UI test doesn't trigger the change of bound property

I have a WPF view and create a radio button on it programmatically:

// 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);

This radio button has a two-way binding to a property in my view model:

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

Now, when I click this radio button in the application, it correctly calls a property setter and sets the value.

The problem is that it doesn't work when I try to do the same from coded UI test:

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

alternativeOptionRadioButton.Selected = true;
Assert.AreEqual(true, viewModel.UseAlternativeOption);

I see that the radio button is selected on the form and can see that Selected property is true, but the viewmodel setter is not being hit. I have put a breakpoint on _useAlternativeOption = value ?? false line and it doesn't get hit, while it is hit when run outside of UI test scenario and clicked manually.

Is there anything I am missing?
How to make RadioButton trigger the change of its Binding within automated UI test scenario?

Aucun commentaire:

Enregistrer un commentaire