user1013607
user1013607

Reputation: 133

Get type of AutomationEvent C# UIAutomation

I was wondering how I could get a specific type of action on AutomationElement? I have all the details when it comes to AutomationElement using

AutomationFocusChangedEventHandler handler = new AutomationFocusChangedEventHandler(OnFocusChange);
Automation.AddAutomationFocusChangedEventHandler(handler);

And there I can get all the information about focused element.

But I need to know if for example button was clicked, if edit input was edited or window was closed and so on.

I know that kind of information is provided in AccEvent and it distinguish events between Property, Focus and Automation with further details each.

Basically I want to record the action performed on element and then repeat it.

How can I get it using C#?

Btw. I use COMwrapper class for UIAutomation.

Upvotes: 1

Views: 857

Answers (1)

DoronG
DoronG

Reputation: 2663

After you get the element from the focus changed event, you can add handlers for button click (invoke), edited (property) and closed. Here's how:

private void OnFocusChange(object sender, AutomationFocusChangedEventArgs e)
{
    var element = sender as AutomationElement;
    if (element == null) return;
    Automation.AddAutomationPropertyChangedEventHandler(element, Treecope.Element, OnChange, AutomationElement.NameProperty, ...);

    if (element.GetSupportedPatterns().Any(p => p.Equals(InvokePattern.Pattern)))
        Automation.AddAutomationEventHandler(InvokePattern.InvokedEvent, element, TreeScope.Element, OnClicked);

    var window = element.Current.ControlType.Equals(ControlType.Window) ? element : GetElementWindow(element);
        Automation.AddAutomationEventHandler(WindowPattern.WindowClosedEvent, window, TreeScope.Element, OnClosed);
}

Now, the qustion of which proprties to subscribe to, really depends on what events your element is sending. This you can view with spy tools like inspect, UISpy, UIAVerify, AccEvent, etc.

Upvotes: 1

Related Questions