'Windows UIAutomation creating PropertyCondition
I am porting some UI automation from the System.Windows.Automation API to the native UIAutomationClient API, and have run into what feels like a simple problem, but cannot find the solution.
I need to find UI elements using code along these lines:
IUIAutomationElement namedElement = currentWindow.FindFirst(UIAutomationClient.TreeScope.TreeScope_Descendants, nameCondition);
Here, the nameCondition object should a IUIAutomationCondition to compare the name of the element to a string.
Using the old System.Windows.Automation I would write this as:
PropertyCondition nameCondition = new PropertyCondition(AutomationElement.NameProperty, "name");
However, I cannot work out how to use the UIAutomation API to create a PropertyCondition. The only examples I have seen seem to suggest something along these lines:
IUIAutomationCondition nameCondition = cuiAutomation.CreatePropertyCondition(propertyIdName, "name");
Here, propertyIdName should be an int representing the NameProperty. However, I cannot work out how to get the correct int here. I assume it must be somewhere in the UIAutomation API as a constant, but cannot find it.
Solution 1:[1]
I discovered the following useful page which lists the values of these property IDs: http://msdn.microsoft.com/en-us/library/windows/desktop/ee684017(v=vs.85).aspx
For example: UIA_AutomationIdPropertyId = 30011
Using this page, I define int constants in my code for the property IDs that I need to use. I don't think that this can be the right answer, but at least it enables me to make progress.
Solution 2:[2]
Not a complete answer, but I found the values in UIAutomationClient.UIA_PropertyIds. Unfortunately, trying to access them directly from your code still results in an error, but you can easily "Go to Definition" on them to grab the id values.
Solution 3:[3]
I have been working quite a while with the IUIAutomation wrapper available as a nuget package for my .net core project. there are some classes that house those infos like PropertyIds, or ControlTypeIds. The nuget in question is Interop.UIAutomationClient. Personally I do not try to go the route of noting down the actual ID, instead I try to use its reference in the code. An example could look something like this:
IUIAutomationCondition cond1 = CurrentWindow.CreatePropertyCondition(UIA_PropertyIds.UIA_ControlTypePropertyId, UIA_ControlTypeIds.UIA_EditControlTypeId);
IUIAutomationCondition cond2 = CurrentWindow.CreatePropertyCondition(UIA_PropertyIds.UIA_NamePropertyId, "File name:");
UIA_ControlTypeIds and UIA_PropertyIds would be the classes referencing the Ids for the different Controltypes and Properties. I am sure there are more of those classes available in the nuget, but personally there was no immediate need to dig in further.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | user304582 |
| Solution 2 | Sam |
| Solution 3 | Mario Mikschovsky |
