Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Controls Do not get distinct automation ID's #2166

Closed
jublin opened this issue Oct 16, 2015 · 12 comments · Fixed by #4508
Closed

Controls Do not get distinct automation ID's #2166

jublin opened this issue Oct 16, 2015 · 12 comments · Fixed by #4508
Labels

Comments

@jublin
Copy link

jublin commented Oct 16, 2015

When using controls such as NumericUpDown, I am unable to get a unique automationID from the control. They all show up as this: The name is being set in the Xaml. Unfortunately I cannot really give a code snippet.
mahappsautiomationid

The problem is my group is trying to test a window with multiple NumericUpDowns and other controls with this same issue. Automating a test becomes impossible to do with this.

@punker76
Copy link
Member

@jublin it seems you should give your controls a x:Name="xyz123" for the automatic tester...

@jublin
Copy link
Author

jublin commented Oct 23, 2015

@punker76 I have done that.

<metroControls:NumericUpDown StringFormat="N0" 
 DataContext="{Binding Source=(Removed)" 
HorizontalAlignment="Left" HorizontalContentAlignment="Left" 
KeyDown="Offset_KeyDown" 
Value="{Binding Offset}" Width="100" x:Name="OffsetControl"  
/>

With Name being defined as x:Name= or Name=, I get

image

The updown buttons also have no name. The overall control does not appear as a focus element.

@punker76
Copy link
Member

@jublin what automatic tool is this?

@jublin
Copy link
Author

jublin commented Oct 23, 2015

@punker76 This is the "VisualUIAVerify" tool from the windows SDK

@punker76
Copy link
Member

@jublin ok, thx, i will take a look after releasing v1.2.0

@jublin
Copy link
Author

jublin commented Oct 26, 2015

This also applies to the Back/down buttons on MetroWindow Flyouts. The automation ID is seen as "nav" for each one. Getting a back button by automationID in testing may not return the intended button for the flyout.

@petvetbr
Copy link
Contributor

@punker76 I'm working on a solution for this

@petvetbr
Copy link
Contributor

petvetbr commented Mar 3, 2016

I´m working on a new implementation based on AutomationPeers. Should be available soon.

@lightwalker01
Copy link

lightwalker01 commented Dec 6, 2023

@punker76, @petvetbr any news on this issue (after 7 years ;-))?
For the moment i have implemented a workaround subclassing NumericUpDown and taking inspiration from DatePickerAutomationPeer original code:

public class AutomationNumericUpDown : NumericUpDown
{
       protected override AutomationPeer OnCreateAutomationPeer()
       {
           if (GenericAutomationPeer.IsEnabled)
           {
               var ap = new AutomationNumericUpDownAutomationPeer(this);
              
               return ap;
           }
           return base.OnCreateAutomationPeer();
       }

       private sealed class AutomationNumericUpDownAutomationPeer : FrameworkElementAutomationPeer, IValueProvider
       {
           private NumericUpDown _owner;

           public AutomationNumericUpDownAutomationPeer(NumericUpDown owner)
               : base(owner)
           {
               _owner = owner;

               var automationContext = owner.GetValue(AutomationHelpers.AutomationContextProperty) as string;
               if (!string.IsNullOrWhiteSpace(automationContext) && owner.Template != null)
               {
                   var txt = owner.Template.FindName("PART_TextBox", owner) as TextBox;
                   if (txt != null)
                   {
                       txt.SetValue(AutomationProperties.AutomationIdProperty, $"txt_{automationContext}");
                   }

                   var up = owner.Template.FindName("PART_NumericUp", owner) as ButtonBase;
                   if (up != null)
                   {
                       up.SetValue(AutomationProperties.AutomationIdProperty, $"btn_{automationContext}.Up");
                   }

                   var down = owner.Template.FindName("PART_NumericDown", owner) as ButtonBase;
                   if (down != null)
                   {
                       down.SetValue(AutomationProperties.AutomationIdProperty, $"btn_{automationContext}.Down");
                   }
               }
           }

           public override object GetPattern(PatternInterface patternInterface)
           {
               if (patternInterface == PatternInterface.Value)
               {
                   return this;
               }

               return base.GetPattern(patternInterface);
           }

           #region IValueProvider 

           bool IValueProvider.IsReadOnly
           {
               get { return false; }
           }

           string IValueProvider.Value
           {
               get { return _owner.Value?.ToString(); }
           }

           void IValueProvider.SetValue(string value)
           {
               if(double.TryParse(value, out double num))
               {
                   _owner.Value = num; 
               }
               else
               {
                   _owner.Value = null;
               }
           }

           #endregion IValueProvider

           protected override string GetLocalizedControlTypeCore() => "numeric up down";

           protected override string GetClassNameCore() => "NumericUpDown";

           protected override AutomationControlType GetAutomationControlTypeCore() => AutomationControlType.Custom;
       }
}

@petvetbr
Copy link
Contributor

petvetbr commented May 1, 2024

@lightwalker01 No news on this front. I was not able to implement this, sorry.

@noubar
Copy link
Contributor

noubar commented Jul 24, 2024

Thank You @lightwalker01 for your post of workaround. This library is really terrible in terms of automation. But your workaround really works

There is also a bug #4454 posted for a really long while which blocks the whole process of test automation, and it is not fixed. I find it unfortunate that we need to implement workarounds.

@timunie
Copy link
Collaborator

timunie commented Jul 24, 2024

@noubar this is a totally free time project and the devs work on things they need. So things are done when they are done. Contribtions are welcome if you want to speed things up.

Thanks for your understanding.

noubar added a commit to noubar/MahApps.Metro that referenced this issue Jul 24, 2024
punker76 added a commit to noubar/MahApps.Metro that referenced this issue Jul 28, 2024
punker76 added a commit that referenced this issue Jul 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

Successfully merging a pull request may close this issue.

6 participants