2015-05-12 27 views
3

我一直對我的頭反對這個問題3天,一直未能找到答案。自定義控件上的AutomationProperties.AutomationId未曝光

我有一個用WPF(dot Net 4.5)編寫的應用程序,我正在用Teststack.White試圖編寫一些自動GUI測試用例。開發人員給了一些控件的x:Names,他們通過Inspect/WPF Inspector/Visual UI Automation顯示驗證AutomationId就好了。

還有其他一些控件被埋沒得更深一些,我負責給自動化id(主要是作爲一個練習,以便我可以更熟悉後端)。這是我一直在抨擊事物的地方。

我試着給控件的AutomationProperties.AutomationId(和.Name)屬性。我給了AutomationProperties一個名稱空間定義。以及我確信SWA.Peers被引用。

我還沒有嘗試在XAML中使用屬性設置器,因爲它們目前沒有多大意義,我希望不需要在C#中編寫東西來設置它們(如果我這樣做,我會執行它但只是希望)。

我的一位同事坐下來,我們拿出了沒有公開自動化ID屬性的最低設置(不幸的是,他和其他開發人員正在爲此未發生空白)。它看起來像:

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); } 
     } 
    } 
} 

我道歉,如果這是一個簡單問題(或者如果我沒有問正確的問題)。我是一個入門級程序員,他對WPF/XAML只有粗略的瞭解。

在此先感謝您的幫助! (希望這是任何簡單的解決方法!)

更新:做一些進一步的挖掘上UI自動化供應商之後,它看起來像所有的自定義控件必須寫入支持AutomationProperties。當然,與開發者交談,這不是案件。

當我找到解決方案時,我會發布更多信息。

+0

你能看到控制UISpy或檢查和的AutomationID ISN」或者你根本找不到控制權? –

回答

4

沒有你的控制AutomationPeer。所以你的AutomationId設置沒有設置任何東西。

覆蓋OnCreateAutomationPeer在你的控制代碼隱藏這樣的:

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

您的新IconLabelButtonAutomationPeer看起來是這樣的:

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

沒有看到這個,謝謝!回覆!這應該使這個過程更容易! –

+0

甜!我把它放到測試項目中,它就像一個魅力! 現在我已經把它放到完整的項目中,看看會發生什麼。再次感謝! –

+0

將IconLabelButtonAutomationPeer類變爲Generic類並將其餘部分適當地更改後,將其放入大型項目中。 工程就像一個魅力! –