Question

given 2 applications:

  • 1 WPF application, where not all controls have a AutomationId, and which I cannot change. In addition the application adds controls at runtime without setting the AutomationId.
  • 1 console application which automates the above WPF application

I need that, because I want to access all elements within nearly the same amount of time. (searching for an automationelement right before using seems to differ a lot (from ms to s) in the time - depending on the amount of elements / and tree-tiers)

I would like to set AutomationIds of WPFs controls within the console application during WPFs runtime. Would be great to know, if you can think of any solution for this problem!


What I have tried until now:

1.)Reflections

Type type = element.GetType();
FieldInfo fi = type.GetField("AutomationIdProperty");
fi.SetValue(element, "x"); //Error

Error message: "Object of type 'System.String' cannot be converted to type 'System.Windows.Automation.AutomationProperty'" But I would like to hand over a value, not a property type...

if I use the following instead, it throws no error, but changes nothing in the XAML

fi.SetValue(element, AutomationElement.AutomationIdProperty); 

2.)directly

AutomationElement element; // = my AutomationElement 
element.Current.AutomationId = "x"; //since AutomationId is Readonly - its not possible

3.) DependencyObjects and DependencyProperties seem also promising, but I couldn't come up with an solution so far. Does someone have experience with that??

Was it helpful?

Solution

IDK how it is possible but WPF Inspector is exactly able to do what I was looking for (now I need to know how they attach to the WPF application :) ).

____OLD ANSWER____ It seems impossible to change the XAML of other programs. If the developers are "too lazy" to set the AutomationId, I've come up with a alternative solution.

The automation app iterates over all controls in the beginning, giving them unique names which are stored in a dictionary, together with their references. In case a component gets added/deleted/changed in the hierarchy, the component and their descendants get deleted in the dictionary and the app re-iterates over this sub-tree again.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top