Kinnara / ModernWpf

Modern styles and controls for your WPF applications
MIT License
4.45k stars 446 forks source link

Is it possible to set the AutomationProperties.AutomationId on various controls? #489

Open aronweiler opened 2 years ago

aronweiler commented 2 years ago

First, let me thank you for the excellent work you've done here on this UI framework- it's a huge improvement over the standard WPF stuff and/or building this myself.

I would, however, like to know if it is possible to set the AutomationId of various controls. I automated the testing of the user interface with WinAppDriver, and like most UI test frameworks, it uses the AutomationId of the controls to find them.

I am using the NuGet version 0.9.6 of your library.

Here is what I am trying to accomplish.

Take this Power Dialog for example:

<ui:ContentDialog x:Class="Application.Views.Dialogs.PowerDialog"
                  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                  xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
                  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                  xmlns:ui="http://schemas.modernwpf.com/2019"
                  xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
                  mc:Ignorable="d"
    Title="Power Off System"
    PrimaryButtonText="Restart"
    SecondaryButtonText="Shutdown"
    CloseButtonText="Cancel"
    DefaultButton="Close"
    PrimaryButtonCommand="{Binding Restart}"
    SecondaryButtonCommand="{Binding Shutdown}"
    AutomationProperties.AutomationId="power dialog">
    <Border>
    </Border>
</ui:ContentDialog>

As you can see, I am setting the AutomationProperties.AutomationId="power dialog" value... however, that AutomationId never makes it to the actual UI (I verify this using the Inspect tool).

I want to set the AutomationIds for the buttons on this dialog, as well, but there does not appear to be a way to accomplish that. The buttons on the dialog DO come with AutomationIds already set, but they don't appear accessible to me. For instance, the cancel button shown above has an AutomationId of CloseButton.

Any help on this would be appreciated.

Thanks,

Aron

aronweiler commented 2 years ago

Some additional information:

I forked your repo and found that I can easily add a DependencyProperty (example below) that allows me to add AutomationIds to the control, but while this works for the buttons that are specified for the ContentDialog, I have as of yet been unable to specify an AutomationId for the title of the dialog.

I did some research around automation and custom WPF controls, and it appears as if there is additional work that probably needs to be done in order to properly flow the AutomationIds from the template into the control itself.

I'm not really a UI guy these days, though, so hopefully you have some idea of what needs to be done in that regard. If you know what the approach is, I can help implement it.

Here's my example of adding and using a custom AutomationId for the buttons on the ContentDialog control:

ContentDialog.cs

public static readonly DependencyProperty PrimaryButtonAutomationIdProperty =
    DependencyProperty.Register(
        nameof(PrimaryButtonAutomationId),
        typeof(string),
        typeof(ContentDialog),
        null);

public string PrimaryButtonAutomationId
{
    get => (string)GetValue(PrimaryButtonAutomationIdProperty);
    set => SetValue(PrimaryButtonAutomationIdProperty, value);
}

ContentDialog.xaml

<Button
    x:Name="PrimaryButton"
    Content="{TemplateBinding PrimaryButtonText}"
    IsEnabled="{TemplateBinding IsPrimaryButtonEnabled}"
    Style="{TemplateBinding PrimaryButtonStyle}"
    AutomationProperties.AutomationId="{TemplateBinding PrimaryButtonAutomationId}"
    HorizontalAlignment="Stretch" />