2

How can i get the actual UWP control element from AutomationElement

Below API returns AutomationElement

parent.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.AutomationIdProperty, automationId));

I want to assert the specific attribute and corresponding value of element. For ex. url of image control.

How can i get the actual control or it's properties? Thanks

Pavan Tiwari
  • 3,077
  • 3
  • 31
  • 71

2 Answers2

1

If it is WPF application then you need to perform following steps to get actual control.

  1. Use UITestControlFactory.FromNativeElement(automationelement, TechnologyName.UIA.ToString()); to convert automationElement into UITestControl. Preceding code will return the instance of UITestControl.
  2. Namespace Microsoft.VisualStudio.TestTools.UITesting.WpfControls provide many control classes for creating instance to access it's properties.You only need to pass object of UITestControl in the constructor. Refer below image

WPF controls

Nitin Sahu
  • 611
  • 6
  • 12
  • Thanks for the answer, is there any equivalent available in UWP ? – Pavan Tiwari Jun 05 '19 at 20:32
  • UWP application also uses XAML so if you inspect the control and It shows technology UIA then WPF automation would also work with UWP. Again it all depends on the way the framework is designed but Microsoft also introduced Window application driver for testing Universal Window Platform(UWP). Refer link https://github.com/Microsoft/WinAppDriver?WT.mc_id=-blog-scottha for more details. I leave it to you to decide. Note: If it answers your question then mark it accepted. – Nitin Sahu Jun 06 '19 at 09:08
-1

First, you have to ensure that the automationId value is matching with your image control. Then you have to cast the result of FindFirst as the ImageControl.

var imageControl = parent.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.AutomationIdProperty, automationId)) as ImageControl;

I would also suggest using more advanced UI Automation libraries like TestStack.White or FlaUI, which has these extension methods where you can pass the condition based on the type of control and the actual Id.

For e.g: Flaui has this API:

var submitBtn= parent.FindFirstDescendant(cf => cf.ByName("SubmitBtn").And(cf.ByControlType(ControlType.Button)))?.AsButton();
SimonG
  • 312
  • 7
  • 20
Sourabh Mishra
  • 212
  • 1
  • 10
  • var imageControl = parent.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.AutomationIdProperty, automationId)) as ImageControl; will produce a compliance error. There is no class inheritance hierarchy between these classes. Explicit casting will also fails CCE. – Pavan Tiwari May 24 '19 at 17:57