I'm trying to automate UI of an extension (WPF) in Visual Studio 2017 using WinApp Driver but cannot find automation id when capturing elements using Inspect.exe (Windows SDK)
Initially there were no automation id's found when inspected by Inspect.exe. So I set the automation id in the XAML file, in its automation properties and installed the new VSIX back in Visual Studio, but still the above set ids were not visible when capturing elements.
But after I attempt to capture the UI objects after setting id/s in the XAML as above described, using inspect element tool, sporadically automation id was appeared but it was not consistent and I could not find the reason why it was not available/visible consistently.
I was succeeded in some scenarios by using 'FindElementByName' and 'FindElementByClassName' but my intention is to use automation id to capture elements more accurately.
Below is the XAML code where I have Set AutomationId;
Button Content="Close" Width="80" Height="25" Margin="10,0,0,0" Visibility="{Binding abc}" Click="CloseButton_Click" AutomationProperties.AutomationId="BtnCloseExample"
Below is the code line where I set the id to capture the element in my test project;
session.FindElementById("BtnCloseExample").Click(); Thread.Sleep(TimeSpan.FromSeconds(3));
I expect a consistent way to set and get the automation id in order to use FindElementById or xpath. Sadly, I could not find good articles related to this issue as well.
Please shed some light and provide some guidelines to achieve this task. It would be utterly valuable if some one can publish a sample of automating WPF application, if possible.
Thank you.
Hi @anunay1 Thanks for the heads up. I will see for a solution with that link.
Easy solution to this. You are too focused on the wrong place. I had that same issue about thinking automation id's were the answer, but they aren't. Also automation id's arent consistent because If the inspect tool cant parse the item in time it may be a problem. Sometimes refreshing the inspect tool works too.
Solution: XPATH, building elements based of XPATH is great. Instantiate once, and just called the variable storing that element with a lambda expression. Build XPATHs based off other unique identifiers. XPATH strings though are a lot faster too when you can minimize the amount of attributes searched.
Hi @Rangyia
I guess you are correct and I will do as you have instructed above. Thanks a lot for the heads up. Appreciate it. Cheers!
Hi @Rangyia , are you sure it is safe enough to use XPath?
Let's say I am inspecting the second label in a pane, I use Label[1].
In the future, if I insert a new Label before this one, now my label is the third, I must remember to update my code to inspect Label[2].
Isn't it risky?
Most helpful comment
This link can help:
https://docs.microsoft.com/en-us/visualstudio/test/set-a-unique-automation-property-for-windows-store-controls-for-testing?view=vs-2019