Home > Software engineering >  UIAutomation FlaUI - Detect opening windows on application level
UIAutomation FlaUI - Detect opening windows on application level

Time:06-08

I am using FlaUI for test automation and it works fine so far. Now I am trying to detect opening windows and while plain UIAutomation offers to register an eventhandler for "AutomationElement.RootElement" I cannot find a way to translate this to FlaUI as it always expects an AutomationElement. I can only think of the MainWindow, but this changes with Splash, Login, Enviroment-Selections, etc.

This is what I have now, but it does not fire when I open a window:

Window win = app.GetMainWindow(automation, TimeSpan.FromSeconds(5));
                UIA3AutomationEventHandler automationEventHandler = new UIA3AutomationEventHandler(win.FrameworkAutomationElement, automation.EventLibrary.Window.WindowOpenedEvent, TestAction);
                automation.NativeAutomation.AddAutomationEventHandler(automation.EventLibrary.Window.WindowOpenedEvent.Id,automation.NativeAutomation.GetRootElement(), (Interop.UIAutomationClient.TreeScope)TreeScope.Descendants, null, automationEventHandler);

Following the working plain UIAutomation code:

Automation.AddAutomationEventHandler(WindowPattern.WindowOpenedEvent,
                AutomationElement.RootElement,
                TreeScope.Descendants,
                someDelegate);

Any help would be highly appreciated! Thanks everybody!

CodePudding user response:

FlaUI has a GetDesktop method on the automation object which is the RootElement. You should be able to register an event there.

CodePudding user response:

With @Roemer' help I made it work. Thank you so much!

using (var automation = new UIA3Automation())
{
            AutomationElement root = automation.GetDesktop();
            UIA3AutomationEventHandler automationEventHandler = new UIA3AutomationEventHandler(root.FrameworkAutomationElement,
                    automation.EventLibrary.Window.WindowOpenedEvent, HandleClickOnceDialogs);
            automation.NativeAutomation.AddAutomationEventHandler(automation.EventLibrary.Window.WindowOpenedEvent.Id, 
                                                            root.ToNative(),
                                                                  (Interop.UIAutomationClient.TreeScope)TreeScope.Descendants,
                                                        null, automationEventHandler);
}

private void HandleClickOnceDialogs(AutomationElement element, EventId id)
{
}
  • Related