0

What is the best way to capture the event of a dialog box opening UIA_WindowControlTypeID(0xC370) (LocalizedControlType = dialog) from a Win32 application an implementation of Windows.System.UIAutomation. I find that using Windows' UIAutomation provided AutomationWindowFocusChangedEvent described in the following, is not very reliable as most of the times it misses the dialog.

 // ..Other automation logic

 // Trigger the success dialog box opening by clicking a button
 GetInvokePattern(exportDialogButton).Invoke();
 Logger.Info("Sleeping for 6s for export to finish and display a success dialog box");
 Thread.Sleep(6000);

 // Handle focus to Success dialog event
 SubscribeToFocusChange();
 Thread.Sleep(8000);
 UnsubscribeFocusChange();

 //Kill process that owns the success dialog - this will forcefully take care of the dialog box by killing the owning process
 process.Kill();

 // Further automation logic..

Event handler object is defined at the class level:

AutomationFocusChangedEventHandler focusHandler = null;

SubscribeToFocusChange() is defined as:

 public void SubscribeToFocusChange()
 {
      focusHandler = new AutomationFocusChangedEventHandler(OnFocusChange);
      Automation.AddAutomationFocusChangedEventHandler(focusHandler);
 }

OnFocusChange is defined as:

private void OnFocusChange(object src, AutomationFocusChangedEventArgs e)
    {
        
        //success dialog
        try
        {
            Logger.Info("Success Dialog: Locating File Exportation dialog");
            AutomationElement feDialog = AutomationElement.RootElement.FindFirst(TreeScope.Descendants, new AndCondition(
                new PropertyCondition(AutomationElement.NameProperty, "* * * File Exportation * * *"),
                new PropertyCondition(AutomationElement.ClassNameProperty, fExportDialogClassProp)
                ));

            //if Success dialog is not null, dismiss by ok'ing it
            if (feDialog != null)
            {
                okButton = feDialog.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.AutomationIdProperty, successPopDismissProp));
                var ok = (InvokePattern)okButton.GetCurrentPattern(InvokePattern.Pattern);
                ok.Invoke();
                Logger.Info("Success Dialog: Found and dismissed");
            }
            else
            {
                Logger.Info("Success Dialog - Not found");
            }
        }
        catch (Exception exc)
        {
            Logger.Info(exc, "Success dialog not found - Encountered unexpected dialog");
        }
    }

UnscubscribeToFocusChange() is defined as:

public void UnsubscribeFocusChange()
{
    if (focusHandler != null)
    {
        Automation.RemoveAutomationFocusChangedEventHandler(focusHandler);
    }
}
StackticMain
  • 211
  • 2
  • 5

0 Answers0