UI Automation to press toolbar button in another application in C#

244 Views Asked by At

I am looking for a method to "click" a toolbar button in another application using the UIAutomation namespace. The other application is not written by me and I don't have access to the source.

I found the parent window using:

AutomationElement _automationElement = AutomationElement.RootElement.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.NameProperty, "Other App"));

I found the toolbar element using:

AutomationElement _toolbarElement = _automationElement.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.NameProperty, "ToolBar1"));

I've tried to navigate further into the toolbar element for decendents and children and they all returned null. Is there a way to access the individual buttons contained within the toolbar?

1

There are 1 best solutions below

1
LM_IT On

I'm working on a similar project, in my case I used this

    private AutomationElement FindButton(AutomationElement parent, string buttonName)
    {
        AutomationElementCollection list = parent.FindAll(TreeScope.Descendants,
               new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Button));
        foreach (AutomationElement element in list)
        {
            if (!string.IsNullOrEmpty(element.Current.Name) && element.Current.Name.Equals("button_name"))
            {
                return element;
            }
        }
        return null;
    }

to find all visible buttons and then put in a variable the need one, however I'm still seeking an adequate method to do the click. At this link there's a method but until now I didn't found the right Mouse class to use. If you can instead, please share.

EDIT: In the end I preferred another approach that worked pretty well

    private void ClickButton(AutomationElement button)
    {
        InvokePattern pat = button.GetCurrentPattern(InvokePattern.Pattern) as InvokePattern;
        pat.Invoke();
    }

The only thing I had to check is if the button have IsInvokePatternAvailable: true with Inspect.exe (part of Windows SDK)