2017-02-21 16 views
0

TestStack White Framework를 사용하여 WPF 응용 프로그램의 테스트 자동화 모달 창을 열고 TextBox에 액세스해야합니다. 모든 것이 잘 작동하지만, 내가 다음 코드 줄이 시도 TestStack White가 WPF 응용 프로그램에서 TextBox를 찾지 못함

의 다른 요소 발견 있지만, 화이트, 텍스트 상자를 찾을 수 없습니다 : CreateBranch 모달 창

입니다

TestStack.White.UIItems.TextBox TextBox = CreateBranch.Get<TestStack.White.UIItems.TextBox>(SearchCriteria.byAutomationId("Title")); 

가 나는 또한 노력을 (SearchCriteria.All), (SearchCriteria.ByControlType)과 아무것도

코딩 된 UI 도구 AutomationID가 아니라이 요소를 발견 작동하지만 화이트

에서 그것을 할 필요가

UISpy 및 기타 유사한 도구는이 컨트롤을 인식하고

이 텍스트 상자는 사용자 지정 컨트롤은, 내가 개인 정보 보호에 대한 네임 스페이스 이름을 변경 여기에 대한 코드입니다 자사의 AutomationID를 참조하십시오

using System; 
using System.ComponentModel; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Data; 
using System.Windows.Input; 
using System.Windows.Interactivity; 
using System.Windows.Media; 

namespace Test.Wpf.Controls.XTextBox 
{ 
    [TemplatePart(Name = "PART_Watermark", Type = typeof(TextBlock))] 
    [TemplatePart(Name = "PART_Pasword", Type = typeof(TextBlock))] 
    public class XTextBox : TextBox 
    { 
     #region Static 
     static XTextBox() 
     { 
      DefaultStyleKeyProperty.OverrideMetadata(typeof(XTextBox), new FrameworkPropertyMetadata(typeof(XTextBox))); 
     } 
     #endregion //Static 

     #region Fields 

     private TextBlock PART_Watermark; 
     private TextBlock PART_Pasword; 

     #endregion //Fields 

     #region DependencyProperties 

     public static readonly DependencyProperty WatermarkProperty = DependencyProperty.Register(
      "Watermark", 
      typeof(String), 
      typeof(XTextBox), 
      new PropertyMetadata(String.Empty)); 

     public static readonly DependencyProperty WatermarkVerticalAlignmentProperty = DependencyProperty.Register(
      "WatermarkVerticalAlignment", 
      typeof(VerticalAlignment), 
      typeof(XTextBox), 
      new PropertyMetadata(VerticalAlignment.Stretch)); 

     public static readonly DependencyProperty WatermarkForegroundProperty = DependencyProperty.Register(
      "WatermarkForeground", 
      typeof(Brush), 
      typeof(XTextBox), 
      new PropertyMetadata(new SolidColorBrush(Colors.Black))); 

     public static readonly DependencyProperty WatermarkFontSizeProperty = DependencyProperty.Register(
      "WatermarkFontSize", 
      typeof(Double), 
      typeof(XTextBox), 
      new PropertyMetadata(12.0)); 

     public static readonly DependencyProperty IsFloatingProperty = DependencyProperty.Register(
      "IsFloating", 
      typeof(Boolean), 
      typeof(XTextBox), 
      new PropertyMetadata(false)); 

     public static readonly DependencyProperty IsAccessNegativeProperty = DependencyProperty.Register(
      "IsAccessNegative", 
      typeof(Boolean), 
      typeof(XTextBox), 
      new PropertyMetadata(true)); 

     public static readonly DependencyProperty IsDigitOnlyProperty = DependencyProperty.Register(
      "IsDigitOnly", 
      typeof(Boolean), 
      typeof(XTextBox), 
      new PropertyMetadata(false)); 

     public static readonly DependencyProperty MaxValueProperty = DependencyProperty.Register(
      "MaxValue", 
      typeof(Single), 
      typeof(XTextBox), 
      new PropertyMetadata(Single.NaN)); 

     public static readonly DependencyProperty IsPasswordProperty = DependencyProperty.Register(
      "IsPassword", 
      typeof(Boolean), 
      typeof(XTextBox), 
      new PropertyMetadata(false)); 

     public static readonly DependencyProperty VisibilityMainTextProperty = DependencyProperty.Register(
      "VisibilityMainText", 
      typeof(Visibility), 
      typeof(XTextBox), 
      new PropertyMetadata(Visibility.Visible)); 

     #endregion //DependencyProperties 

     #region Properties 
     [Description("Gets or sets the watermark title")] 
     public String Watermark 
     { 
      get { return (String)GetValue(WatermarkProperty); } 
      set { SetValue(WatermarkProperty, value); } 
     } 

     [Description("Gets or sets the watermark vertical alignment")] 
     public VerticalAlignment WatermarkVerticalAlignment 
     { 
      get { return (VerticalAlignment)GetValue(WatermarkVerticalAlignmentProperty); } 
      set { SetValue(WatermarkVerticalAlignmentProperty, value); } 
     } 


     [Description("Gets or sets the watermark title color")] 
     public Brush WatermarkForeground 
     { 
      get { return (Brush)GetValue(WatermarkVerticalAlignmentProperty); } 
      set { SetValue(WatermarkVerticalAlignmentProperty, value); } 
     } 

     [Description("Gets or sets the watermark title font size")] 
     public Double WatermarkFontSize 
     { 
      get { return (Double)GetValue(WatermarkVerticalAlignmentProperty); } 
      set { SetValue(WatermarkVerticalAlignmentProperty, value); } 
     } 

     [Description("Gets or sets the textbox floating mode")] 
     public Boolean IsFloating 
     { 
      get { return (Boolean)GetValue(IsFloatingProperty); } 
      set { SetValue(IsFloatingProperty, value); } 
     } 

     [Description("Gets or sets the textbox access of negative values")] 
     public Boolean IsAccessNegative 
     { 
      get { return (Boolean)GetValue(IsAccessNegativeProperty); } 
      set { SetValue(IsAccessNegativeProperty, value); } 
     } 

     [Description("Gets or sets the textbox chars type")] 
     public Boolean IsDigitOnly 
     { 
      get { return (Boolean)GetValue(IsDigitOnlyProperty); } 
      set { SetValue(IsDigitOnlyProperty, value); } 
     } 

     [Description("Gets or sets the max input value (enable in digit mode only)")] 
     public Single MaxValue 
     { 
      get { return (Single)GetValue(MaxValueProperty); } 
      set { SetValue(MaxValueProperty, value); } 
     } 

     [Description("Gets or sets the textbox is passwordbox")] 
     public Boolean IsPassword 
     { 
      get { return (Boolean)GetValue(IsPasswordProperty); } 
      set { SetValue(IsPasswordProperty, value); } 
     } 

     public Visibility VisibilityMainText 
     { 
      get { return (Visibility)GetValue(VisibilityMainTextProperty); } 
      set { SetValue(VisibilityMainTextProperty, value); } 
     } 

     #endregion //Properties 

     public override void OnApplyTemplate() 
     { 
      base.OnApplyTemplate(); 
      PART_Watermark = GetTemplateChild("PART_Watermark") as TextBlock; 
      PART_Pasword = GetTemplateChild("PART_Pasword") as TextBlock; 
      SetWatermarkVisibility(); 
      if (IsPassword) 
      { 
       VisibilityMainText = Visibility.Collapsed; 
       if (PART_Pasword != null) 
       { 
        PART_Pasword.Visibility = Visibility.Visible; 
        PART_Pasword.FontSize = 20; 
       } 
      } 
      else 
      { 
       VisibilityMainText = Visibility.Visible; 
      } 

      DataObject.AddPastingHandler(this, OnPaste); 
     } 

     protected void OnPaste(Object sender, DataObjectPastingEventArgs e) 
     { 
      try 
      { 
       var isText = e.SourceDataObject.GetDataPresent(DataFormats.UnicodeText, true); 
       if (!isText) return; 
       var text = e.SourceDataObject.GetData(DataFormats.UnicodeText) as String; 
       if (!String.IsNullOrEmpty(text)) 
       { 
        if (IsDigitOnly) 
        { 
         if (!IsAccessNegative) 
         { 
          var ch = text[0]; 
          if (ch == 45) 
          { 
           e.CancelCommand(); 
          } 
         } 
         for (int i = 0; i < text.Length; i++) 
         { 
          if (i == 0) 
          { 
           if (IsAccessNegative && text[i] == 45) 
           { 
            continue; 
           } 
          } 
          if (!Char.IsDigit(text[0])) 
           e.CancelCommand(); 
         } 
        } 
       } 
      } 
      catch (Exception) 
      { 
       // ignored 
       e.Handled = true; 
      } 
     } 

     protected override void OnTextChanged(TextChangedEventArgs e) 
     { 
      base.OnTextChanged(e); 
      SetWatermarkVisibility(); 
      if (IsPassword) 
      { 
       PART_Pasword.Text = new String('•', Text.Length); 
      } 
     } 

     protected override void OnLostKeyboardFocus(KeyboardFocusChangedEventArgs e) 
     { 
      base.OnLostKeyboardFocus(e); 
      SetWatermarkVisibility(); 
     } 

     protected override void OnGotKeyboardFocus(KeyboardFocusChangedEventArgs e) 
     { 
      base.OnGotKeyboardFocus(e); 
      if (PART_Watermark != null) 
      { 
       PART_Watermark.Visibility = Visibility.Hidden; 
      } 
     } 

     protected override void OnPreviewTextInput(TextCompositionEventArgs e) 
     { 
      base.OnPreviewTextInput(e); 
      if (e.Text.Length == 0) 
      { 
       e.Handled = true; 
       return; 
      } 
      if (IsDigitOnly) 
      { 
       var ch = e.Text[0]; 
       if (!Char.IsDigit(ch) && ch != 8 && ch != 46) 
       { 
        if (!(IsAccessNegative && ch == 45)) 
         e.Handled = true; 
       } 
       if (IsFloating) 
       { 
        if (ch == 46 && Text.IndexOf('.') != -1) 
        { 
         e.Handled = true; 
         return; 
        } 
       } 

       if (!IsAccessNegative) 
       { 
        if (ch == 45) 
        { 
         e.Handled = true; 
        } 
       } 
      } 
     } 

     #region Private 

     private void SetWatermarkVisibility() 
     { 
      if (PART_Watermark != null) 
      { 
       PART_Watermark.Visibility = (Text != String.Empty || IsKeyboardFocused)? Visibility.Hidden : Visibility.Visible; 
      } 
     } 
     #endregion 
    } 
} 

Screenshot from UISpy

+0

당신이 윈도우뿐만 아니라 UISpys 출력의 스크린 샷을 찾기위한 코드를 게시 할 수 없습니다 문제의 텍스트 상자? 나는 일반적으로 창문에서 컨트롤을 찾을 수 없을 때 실수로 다른 창을 발견했다. 일반적으로 창은 도구 팁 또는 유사한 것으로 끝납니다. –

+0

응용 프로그램 = TestStack.White.Application.Launch (ApplicationPath); TestStack.White.UIItems.WindowItems.Window mainWindow = application.GetWindow (SearchCriteria.ByText ("title"), TestStack.White.Factory.InitializeOption.NoCache); // 모달 창을 여는 동작과 열 때 동작 TestStack.White.UIItems.WindowItems.Window CreateBranch = mainWindow.ModalWindow (SearchCriteria.All); 스크린 샷을 추가했습니다. 편집에는 두 개의 스크롤바가 있지만 사용되지 않습니다. AutomationPeer를 추가 할 때 DLDR의 조언을 사용하려고 시도했지만 편집이 아닌 사용자 지정 컨트롤이 인식되었습니다. –

답변

0

나를 보자 그

TextBox = (TextBox)CreateBranch 
     .Get(SearchCriteria.ByAutomationId("Title").AndOfFramework(WindowsFramework.Wpf)); 

에드 작동하는지 알고 ited 후 새로운 소스 당신은 사용자 지정 컨트롤에 대한 특정 AutomationPeer을 만들고 방법 OnCreateAutomationPeer()의 재정의를 통해 그것을 반환해야

을 추가했다.

컨트롤은 TextBox 컨트롤의 하위 클래스이므로 새로운 TextBoxAutomationPeer 인스턴스를 반환하거나 인스턴스에서 사용자 지정 AutomationPeer를 만들 수 있습니다.

public class XTextBox : TextBox 
{ 
... 
    protected override AutomationPeer OnCreateAutomationPeer() 
    { 
     return new XTextBoxAutomationPeer(this); 
     // or just use the TextBoxAutomationPeer 
     // return new TextBoxAutomationPeer(this); 
    } 
... 
} 

사용자 정의 자동화 피어

public class XTextBoxAutomationPeer : TextBoxAutomationPeer 
{ 
    public XTextBoxAutomationPeer(XTextBox owner) 
     : base(owner) 
    { 
    } 

    protected override string GetClassNameCore() 
    { 
     return "XTextBox"; 
    } 
} 
+0

아니요, 작동하지 않습니다. –

+0

텍스트 상자를 사용자 지정 컨트롤입니까? XAML 코드를 제공 할 수 있습니까?여기 – DLDR

+0

는 XAML 코드 : 을 "... 여기에 제목을 입력합니다"그리고 XTextBox이있다. cs이므로 사용자 지정 컨트롤입니다. 표준 제어로 어떻게 작동합니까? 아니면 다른 작업을 수행 할 수 있습니까? –

0
[SetUpFixture] 
    public class SETUP_THAT_WILL_GET_CALL_LATER 
    { 
     [OneTimeSetUp] 
     public void OneTimeSetUp() 
     { 
      var applicationDirectory = TestContext.CurrentContext.TestDirectory; 
      var applicationPath = Path.Combine(applicationDirectory, @"..\..\..\, "your debug folder path here", "your application.exe here"); 
      Application = Application.Launch(applicationPath); 

      Thread.Sleep(2000); 

      Window = Application.GetWindow("Title of your application", InitializeOption.WithCache); 
     } 

     [OneTimeTearDown()] 
     public void OneTimeTearDown() 
     { 
      Window.Dispose(); 
      Application.Dispose(); 
     } 

     public static Application Application; 
     public static Window Window; 
    } 
테스트에서 그런

[Test] 
public void yourtest() 
{ 
    var textBox = SETUP_THAT_WILL_GET_CALL_LATER.**Window.Get<TextBox>("Your textbox name here");** 

} 
+0

그것은 윈도우 응용 프로그램을 시작하고 얻기를 위해, 내 코드처럼 보이지만, 잠도 –

+0

흠 threadsleep을하려고 도움이되지 않습니다를 추가 한 원래의 질문 – user7583356

+0

매 단계마다 더 오래 잠자는 것이 도움이되지 않습니다. –