1

I am doing some experimentation with Windows Automation. It is recommended not to use the System.Windows.Automation managed code anymore due to updated patterns in windows 7 & 8 and to use the new COM library. I have found a wrapper called UIAComWrapper on NuGet. I have a piece of code below that invokes a login button on a window. I have tried invoking on some other windows with buttons as well. When using the System.Windows.Automation library the code works fine but when using the UIAComWrapper I get an exception 2-3 seconds after it has successfully invoked the button. Here is the code:

    static void Main(string[] args)
    {
        Automation.AddAutomationEventHandler(WindowPattern.WindowOpenedEvent, AutomationElement.RootElement, TreeScope.Subtree, (sender, e) =>
        {

            AutomationElement src = sender as AutomationElement;
            if (src != null && src.Current.Name == "Login Tester")
            {
                Console.WriteLine("Class : " + src.Current.ClassName);
                Console.WriteLine("Title : " + src.Current.Name);
                Console.WriteLine("Handle : " + src.Current.NativeWindowHandle);
                Console.WriteLine("ProcessId : " + src.Current.ProcessId);
                Console.WriteLine("ControlType : " + src.Current.ControlType);
                Console.WriteLine("AutomationId : " + src.Current.AutomationId);


                Condition conditions = new AndCondition(
                    new PropertyCondition(AutomationElement.IsEnabledProperty, true),
                    new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Button)
                    );

                AutomationElementCollection elementCollection = src.FindAll(TreeScope.Children, conditions);

                foreach (AutomationElement ae in elementCollection)
                {
                    if (ae.Current.AutomationId == "btnLogin")
                    {
                        InvokePattern ip = ae.GetCurrentPattern(InvokePattern.Pattern) as InvokePattern;
                            ip.Invoke();

                    }
                }
            }
        });

        Console.ReadLine();
    }
}   

And here is the exception:

  Error HRESULT E_FAIL has been returned from a call to a COM component.

And here is the stacktrace:

at UIAutomationClient.IUIAutomationInvokePattern.Invoke() at System.Windows.Automation.InvokePattern.Invoke() in d:\dev\UIAComWrapper\UiaComWrapper\InvokePattern.cs:line 37 at uiacomwrapperexperimentation2.Program.<>c.<Main>b__0_0(Object sender, AutomationEventArgs e) in C:\Users\****\Documents\Visual Studio 2017\Projects\uiacomwrapperexperimentation2\uiacomwrapperexperimentation2\Program.cs:line 40 at UIAComWrapperInternal.BasicEventListener.UIAutomationClient.IUIAutomationEventHandler.HandleAutomationEvent(IUIAutomationElement sender, Int32 eventId) in d:\dev\UIAComWrapper\UiaComWrapper\ClientEventList.cs:line 111

I have read some posts that say this error could be due to project cache and have tried those solutions with no avail. Here is the Git for the wrapper https://github.com/TestStack/UIAComWrapper

JMIII
  • 320
  • 2
  • 16
  • The original repository for UIAComWrapper is this https://uiacomwrapper.codeplex.com/ can you test this one? It may be exactly the same, I've not tested if they are different. – Simon Mourier Nov 17 '17 at 07:15
  • 1
    @SimonMourier I just tested and still same error. I am going to try on another workstation today and see if anything different occurs. – JMIII Nov 17 '17 at 14:21
  • @SimonMourier when I use the EventTests.TestInvokeEvent() it works by getting the start button without error. Only when I use to grab a button from running app do I get the exception. – JMIII Nov 17 '17 at 15:53
  • @SimonMourier Ok so I setup a windows 8.1 dev workstation and did same test with same application and it worked fine? I have noticed it only fails on Windows 7 when invoking the button triggers another msg box to open. It appears it locks at the invoke while the msg box is open. – JMIII Nov 17 '17 at 18:00
  • It maybe a bug somewhere in Windows or UIA... Microsoft doesn't care much about Windows 7 these days, so I doubt you'll find a fix. – Simon Mourier Nov 17 '17 at 18:07
  • LOL that is very true. – JMIII Nov 17 '17 at 19:14
  • 1
    You might want to consider using FlaUI's core https://github.com/Roemer/FlaUI instead of the UIAComWrapper. I have been using this for quite some time and haven't had the issues I have had with the UIAComWrapper. The core of FlaUI is just another wrapper around the UIAv3 COM dll. – Max Young Dec 03 '17 at 03:41
  • Thanks will look at and test. – JMIII Dec 03 '17 at 13:33
  • @MaxYoung before I start pounding away into it. I read first page on git and demo's were launching applications. Are you able to attach to existing open applications with this library? – JMIII Dec 04 '17 at 13:55
  • 2
    Yeah the `Application` class has a static method for attaching https://github.com/Roemer/FlaUI/blob/master/src/FlaUI.Core/Application.cs – Max Young Dec 04 '17 at 19:39

0 Answers0