I have an event in my WPF application that looks like this:
private void ShiftsDataGrid_OnBeginningEdit(object? sender, DataGridBeginningEditEventArgs e)
{
if (e.Column == ShiftIdColumn && !e.Row.IsNewItem)
{
CancelColorPopup();
e.Cancel = true;
MessageBox.Show("Its not allowed to edit an existing id for shifts");
}
}
It is meant to block users from editing a cell in a datagrid in certain situations and show a messagebox when it happens.
Im currently building a UI test via System.Windows.Automation
where in my code I currently have:
var task = Task.Run(() =>
{
var cell = mainWindow.FindDescendantById("ShiftsDataGrid_Row0_Column0");
var parentCell = cell.GetParent();
parentCell.InsertValue("0");
});
WaitForCondition(() =>
{
AutomationElement.
var okButton = AutomationElement.RootElement.FindFirst(TreeScope.Children,
new PropertyCondition(AutomationElement.AutomationIdProperty, "2"));
if (okButton == null)
return false;
okButton.Click();
return true;
}, TimeSpan.FromSeconds(200));
This is one of many attempts to provoke the message box and try to click the button. The problem comes with the call parentCell.InsertValue("0");
This specific call, engages the edit event code above and showing the message box as expected. What it also does is block the testing code from progressing. If I click the button manually, it will release the parentCell.InsertValue("0");
During this block, I have tried in other threads to find this MessageBox, but with no success. No matter what I do, I cannot find the AutomationIdProperty of 2
, which is the Ok button for the messagebox. I have verified this with Accessibility Insight for Windows.
My guess is the UI thread is blocking here, but I am not certain. Does anyone have a good approach on how to handle this situation?