Search code examples
c#automationui-automationmicrosoft-ui-automation

How to use UiAutomationClient Without UIAComWrapper Causing Conflict?


I am working on building some ui automation in c# using UIAutomationClient. It is my understanding that this requires NuGet UIAComWrapper as well. Without UIAComWrapper, the decleration 'using UIAutomationClient' has error:

The type or namespace name 'UIAutomationClient' could not be found (are you missing a using directive or an assembly reference?)

Adding UIAComWrapper as a reference corrects the above error, which to me supports that they must both be added as references.

However, now when attempting to make use of a variety of methods(?) of this reference, I get errors for the Method existing in both references:

Error   CS0433  The type 'AutomationElementCollection' exists in both 'UIAComWrapper, Version=1.1.0.14, Culture=neutral, PublicKeyToken=78cbcf77433a85e5' and 'UIAutomationClient, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'   

From what I can find, when the type exists in multiple references, you must give a fully qualified name to the reference to utilize:

System.UIAuatomationClient.AutomationElementCollection installBaseChildren = installBaseWindow.FindAll....

Or I have seen that you can declare an alias as shorthand for this same approach:

using UIA = UIAutomationClient;

But I'm wondering if I'm overcomplicating this all, and there might be a way to make use of UIAutomationClient that doesn't require all of this additional configuration to avoid conflict between references?

Additionally, I'm new to using c# for this type of automation - if there's something newer/better for c# I should consider, I'll take that feedback as well.


Solution

  • UIAutomationClient is the name of the original dll that is provided by the .NETFramework and lives in the GAC. It's also can be referred as Windows Automation API 2.0. If you are targeting .NETFramework, you can add a framework reference and be set to use UI Automation. Nothing else needed!

    While most dlls follow that the namespaces contained in the dll are the same, UIAutomationClient does not. The main namespace you are going to use is System.Windows.Automation. If you are using Visual Studio, you can browse the dll using the object explorer (Ctrl+Alt+J).

    The NuGet package UIAComWrapper was made so you can access the Windows Automation COM API 3.0 which is faster and more reliable. You can browse the internals of this nuget package here. Notice that it has the UIAutomationClient namespace but it's provided by the COM interop dll. That's why your using UIAutomationClient error goes away when you add this package.

    enter image description here

    The second problem you are facing is clashing types and this is because almost all of the types in the .NETFramework UIAutomationClient.dll also exist in the NuGet package UIAComWrapper. Use one or the other but not both.

    Finally, are you overcomplicating this? Not really but there is a much better solution. FlaUI.
    This library is amazingly fast with UI automation. I personally use it for my integration tests at work. Check out the GitHub unit tests, samples, and wiki and you'll be set for UI Automation.