question

EswaranT-7225 avatar image
0 Votes"
EswaranT-7225 asked EswaranT-7225 edited

In UWP, How to set the automation ID for the custom control?

I have created a custom control in Xamarin Forms. For testing, I used the automation ID property in forms but I am unable to get the automation id in the inspect tool. If I set a dummy native control, I am able to get the automation property but if I didn't set the native control the automation id is not shown. Setting a dummy native control is not a good practice so anyone please help how to get the automation id for the custom control. I have attached my sample below:
FormsCustomControl.zip



The code snippet is provided below:

 class FormsCustomLayout : TemplatedView
     {
         public static readonly BindableProperty InputViewProperty =
             BindableProperty.Create(nameof(InputView), typeof(View), typeof(FormsCustomLayout), null, BindingMode.Default, null, OnInputViewChanged);
    
         private static void OnInputViewChanged(BindableObject bindable, object oldValue, object newValue)
         {
             (bindable as FormsCustomLayout).OnInputViewChanged(oldValue, newValue);
         }
    
         private void OnInputViewChanged(object oldValue, object newValue)
         {
             var oldView = (View)oldValue;
             if (oldView != null)
             {
                 if (this.contentGrid.Children.Contains(oldView))
                 {
                     oldView.SizeChanged -= OnInputViewSizeChanged;
                     oldView.BindingContext = null;
                     this.contentGrid.Children.Remove(oldView);
                 }
             }
    
             var newView = (View)newValue;
             if (newView != null)
             {
                 newView.SizeChanged += OnInputViewSizeChanged;
                 newView.VerticalOptions = LayoutOptions.CenterAndExpand;
                 newView.HeightRequest = 60;
                  this.contentGrid.Children.Add(newView);
             }
         }
    
         private void OnInputViewSizeChanged(object sender, EventArgs e)
         {
    
         }
    
         public  View InputView
         {
             get { return (View)GetValue(InputViewProperty); }
             set { SetValue(InputViewProperty, value); }
         }
    
         internal void UpdateText(object text)
         {
             this.Text = (string)text;
         }
    
         private readonly Grid contentGrid = new Grid();
    
         public FormsCustomLayout()
         {
             this.ControlTemplate = new ControlTemplate(typeof(StackLayout));
             ((StackLayout)Children[0]).Children.Add(this.contentGrid);
             if (InputView != null)
             {
                 this.contentGrid.Children.Add(InputView);
             }
         }
    
         internal string Text { get; private set; }
     }

The custom renderer code snippet is provided below:

 class FormsCustomLayoutRenderer : ViewRenderer<FormsCustomLayout, FrameworkElement>
     {

         /// <summary>
         /// Method that is called when the automation id is set.
         /// </summary>
         /// <param name="id">The automation id.</param>
         protected override void SetAutomationId(string id)
         {
             if (this.Control == null)
             {
                 base.SetAutomationId(id);
             }
             else
             {
                 this.SetAutomationPropertiesAutomationId(id);
                 this.Control.SetAutomationPropertiesAutomationId(id);
             }
         }
    
         /// <summary>
         /// Provide automation peer for the control
         /// </summary>
         /// <returns>The TextInputLayout view automation peer.</returns>
         protected override AutomationPeer OnCreateAutomationPeer()
         {
             if (this.Control == null)
             {
                 return new FormsCustomLayoutAutomationPeer(this);
             }
    
             return new FormsCustomLayoutAutomationPeer(this.Control);
         }
    
         protected override void OnElementChanged(ElementChangedEventArgs<FormsCustomLayout> e)
         {
             base.OnElementChanged(e);
             var element = e.NewElement;           
         }           
     }
     internal class FormsCustomLayoutAutomationPeer : FrameworkElementAutomationPeer
     {
         /// <summary>
         /// Initializes a new instance of the <see cref="FormsCustomLayoutRenderer"/> class.
         /// </summary>
         /// <param name="owner">FormsCustomLayout View control</param>
         public FormsCustomLayoutAutomationPeer(FrameworkElement owner) : base(owner)
         {
         }
    
         /// <summary>
         /// Describe the control type
         /// </summary>
         /// <returns>The control type.</returns>
         protected override AutomationControlType GetAutomationControlTypeCore()
         {
             return AutomationControlType.Custom;
         }
    
         /// <summary>
         /// Describe the class name.
         /// </summary>
         /// <returns>The Class Name.</returns>
         protected override string GetClassNameCore()
         {
             return "FormsCustomLayout";
         }
     }
dotnet-xamarinwindows-uwp
· 6
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi,EswaranT-7225
Can you provide the test code ? We need to reproduce the problem for further troubleshooting.

0 Votes 0 ·

Hi @ColeXia-MSFT,

Sorry for the late reply. In the above question also I have added my code and also the code snippet is available in the FormsCustomControl sample and the sample link is provided below: https://github.com/Eswaran17392/Test

Regards,
Eswaran

0 Votes 0 ·

@EswaranT-7225

Are you using UI-Test or Accessibility ?

0 Votes 0 ·
Show more comments

1 Answer

NicoleLu-9366 avatar image
0 Votes"
NicoleLu-9366 answered EswaranT-5933 edited

@EswaranT-7225 according to https://github.com/microsoft/appcenter/issues/25#issuecomment-468646604, currently UITest does not include support for UWP, please consider Appium for UWP test. Also you can vote up for this github feature request to add priority to the team :)

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @NicoleLu-9366,

Our customer using the Appium tool for testing the UWP application. For that, need AutomationID for our custom control. But we don't know how to set Automation ID to native UWP control from Forms.UWP renderer project. Hence, raised the query in the forum.

0 Votes 0 ·