AutomationProperties.AutomationId is not subject to custom control

I've been banging my head about this issue for three days now and haven't found an answer.

I have an application written in WPF (point Net 4.5) and I am working with Teststack.White trying to write some automated GUI test cases. The developers have provided x: Names for some of the controls and they show up via Inspect / WPF Inspector / Visual UI Automation Verify as AutomationId is just fine.

There are other controls that are a little buried in, and I was tasked with providing Automation IDs (mostly as an exercise so I can get a better understanding of the inner end). That's where I hit my head on the thing.

I tried to give the controls an AutomationProperties.AutomationId (and .Name) attribute. I gave AutomationProperties a namespace definition. Also, I made sure the SWA.Peers links are listed.

I haven't tried using the XAML property definitions as they don't make a lot of sense at the moment and I hope they don't need to write stuff in C # to set them (if I do, I'll do it, but just hoping).

One of my co-workers came up and we pulled out a minimal setup that does not expose the Automation ID property (unfortunately, he, like other developers, draws an empty question about why this is not happening) .It looks like this:

MainWindow.xaml:

<Window x:Class="TestAutomationUI.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:autoProp="clr-namespace:System.Windows.Automation;assembly=PresentationCore"
    xmlns:common="clr-namespace:Asi.Ui.Common"
    Title="Test UI (Pick me)" Height="455.075" Width="525">

    <Window.Resources>
        <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="/IconLabelButton.xaml" />
        </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Window.Resources>

    <StackPanel x:Name="PresentationRoot">
        <common:IconLabelButton x:Name="TestButton" autoProp:AutomationProperties.AutomationId="TestButtonClick" Text="Stuff" Margin="245,0,214.4,0" RenderTransformOrigin="4.648,0.588" Height="32">
        </common:IconLabelButton>
    </StackPanel>
</Window>

      

IconLabelButton.xaml:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:autoProp="clr-namespace:System.Windows.Automation;assembly=PresentationCore"
                    xmlns:common="clr-namespace:Asi.Ui.Common">

    <Style TargetType="common:IconLabelButton">
        <Setter Property="Template" Value="{DynamicResource Asi.Ui.Common.IconLabelButton}" />
        <Setter Property="IsTabStop" Value="False" />
        <Setter Property="Background" Value="Transparent" />
        <Setter Property="FontWeight" Value="{DynamicResource Mobius.UI.Resources.Fonts.WeightLight}"/>
        <Setter Property="Spacing" Value="10" />
    </Style>

    <ControlTemplate x:Key="Asi.Ui.Common.IconLabelButton" TargetType="common:IconLabelButton">
        <Border Background="{TemplateBinding Background}" Height="30">
            <Button Style="{DynamicResource Mobius.UI.Resources.Styles.IconButton}" Margin="0" Padding="0" HorizontalContentAlignment="Stretch" HorizontalAlignment="Left"
                    Command="{TemplateBinding Command}" CommandParameter="{TemplateBinding CommandParameter}" Foreground="{TemplateBinding Foreground}">

                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="22" />
                        <ColumnDefinition Width="{TemplateBinding Spacing}" />
                        <ColumnDefinition Width="*" />
                    </Grid.ColumnDefinitions>
                    <Grid Grid.Column="0" HorizontalAlignment="Left" VerticalAlignment="Top" Width="22" Height="22">
                        <Path Margin="1" Height="20" Width="20" Fill="{Binding Foreground, RelativeSource={RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type ContentControl}}}" Data="{TemplateBinding Icon}" Stretch="Fill"/>
                        <Path Margin="1" Height="20" Width="20" Fill="{TemplateBinding AdornerIconFill}" Data="{TemplateBinding AdornerIcon}" Stretch="Fill"/>
                    </Grid>
                    <TextBlock Grid.Column="2" Text="{TemplateBinding Text}" VerticalAlignment="Center" Foreground="{Binding Foreground, RelativeSource={RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type ContentControl}}}" FontFamily="{TemplateBinding FontFamily}" FontSize="{TemplateBinding FontSize}" FontStretch="{TemplateBinding FontStretch}" FontWeight="{TemplateBinding FontWeight}"/>
                </Grid>
            </Button>
        </Border>
    </ControlTemplate>

</ResourceDictionary>

      

IconLabelButton.xaml.cs:

    using System;
using System.Windows;
using System.Windows.Automation;
using System.Windows.Automation.Peers;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;

namespace Asi.Ui.Common
{

    public class IconLabelButton : Control
    {

        public static readonly DependencyProperty TextProperty =
            DependencyProperty.Register("Text", typeof(string), typeof(IconLabelButton), new PropertyMetadata(default(string)));

        public static readonly DependencyProperty AdornerIconProperty =
            DependencyProperty.Register("AdornerIcon", typeof(object), typeof(IconLabelButton), new PropertyMetadata(default(object)));

        public static readonly DependencyProperty IconProperty =
            DependencyProperty.Register("Icon", typeof(object), typeof(IconLabelButton), new PropertyMetadata(default(object)));

        public static readonly DependencyProperty CommandProperty =
            DependencyProperty.Register("Command", typeof(ICommand), typeof(IconLabelButton), new PropertyMetadata(default(ICommand)));

        public static readonly DependencyProperty CommandParameterProperty =
            DependencyProperty.Register("CommandParameter", typeof(object), typeof(IconLabelButton), new PropertyMetadata(default(ICommand)));

        public static readonly DependencyProperty SpacingProperty =
            DependencyProperty.Register("Spacing", typeof(GridLength), typeof(IconLabelButton), new PropertyMetadata(default(GridLength)));

        public static readonly DependencyProperty IconButtonSizeProperty =
            DependencyProperty.Register("IconButtonSize", typeof(GridLength), typeof(IconLabelButton), new PropertyMetadata(default(GridLength)));

        public static readonly DependencyProperty AdornerIconFillProperty =
            DependencyProperty.Register("AdornerIconFill", typeof(Brush), typeof(IconLabelButton), new PropertyMetadata(default(Brush)));


        static IconLabelButton()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(IconLabelButton), new FrameworkPropertyMetadata(typeof(IconLabelButton)));
        }

        public string Text
        {
            get { return (string)GetValue(TextProperty); }
            set { SetValue(TextProperty, value); }
        }
        public object AdornerIcon
        {
            get { return GetValue(AdornerIconProperty); }
            set { SetValue(AdornerIconProperty, value); }
        }
        public object Icon
        {
            get { return GetValue(IconProperty); }
            set { SetValue(IconProperty, value); }
        }
        public ICommand Command
        {
            get { return (ICommand)GetValue(CommandProperty); }
            set { SetValue(CommandProperty, value); }
        }
        public object CommandParameter
        {
            get { return GetValue(CommandParameterProperty); }
            set { SetValue(CommandParameterProperty, value); }
        }
        public GridLength Spacing
        {
            get { return (GridLength)GetValue(SpacingProperty); }
            set { SetValue(SpacingProperty, value); }
        }
        public GridLength IconButtonSize
        {
            get { return (GridLength)GetValue(IconButtonSizeProperty); }
            set { SetValue(IconButtonSizeProperty, value); }
        }
        public Brush AdornerIconFill
        {
            get { return (Brush)GetValue(AdornerIconFillProperty); }
            set { SetValue(AdornerIconFillProperty, value); }
        }
    }
}

      

I apologize if this is a simple question (or if I am not asking the correct one). I am an entry-level programmer who only has a cursory acquaintance with WPF / XAML.

Thanks in advance for your help! (Hope this is easy to fix!)

Update: . After further digging into the UI Automation providers, it looks like all custom controls should be written to support AutomationProperties. Of course, talking to developers, which is not the case.

I will post more information when I find out that a solution has been found.

+3


source to share


1 answer


For your control, no AutomationPeer

. Therefore your installation AutomationId

doesn't install anything.

Override OnCreateAutomationPeer

in your control code as follows:

    protected override AutomationPeer OnCreateAutomationPeer()
    {
        return new IconLabelButtonAutomationPeer(this);
    }

      



where your new one IconLabelButtonAutomationPeer

looks like this:

public class IconLabelButtonAutomationPeer : FrameworkElementAutomationPeer
{
    public IconLabelButtonAutomationPeer(IconLabelButton owner)
        : base(owner)
    {
    }
}

      

+4


source







All Articles