locked
Problem testing validation in viewmodel using coded ui test RRS feed

  • Question

  • I am new to Coded UI test, and hope to test a large UI fully and completely (of course) using Coded UI testing.

    During my initial researchs, I ran into problems when trying to test the Validation.HasError property of TextBox controls and similar. And since this is crusial for a complete testing, I have to find a solution.

    The problem is to test the value of HasError and to test if an adorner decorator is visible above the control. By first glance this seems not to be possible to do directly in Coded UI test.

    Perhaps I need to use some extensions ?

    Any suggestions are welcome.

     

    Tuesday, December 11, 2012 5:31 PM

Answers

  • The adorner decorator above the TextBox is the standard red error border, which is displayed when the textbox is invalidated through binding.

    The crosshair tool in the Coded UI test builder does not find this border, it finds the TextBox below.

    I think, I have to give up on this approach, and instead try to get information about the red border through the adorner layer, and code this into the test.

    Thursday, December 13, 2012 9:31 AM

All replies

  • Just curious what does SPY++ show for adorner decorator?

    JP Cowboy Coders Unite!

    Wednesday, December 12, 2012 12:25 AM
  • Hello WPFASL,

    Thank you for your post.

    As Mr.Javaman said, I would like to know what the adorner decorator above the TextBox control is.

    If possible, you can provide us a screenshot about your application UI.

    In addition, I suggest that you can drag the crosshair on the adorner decorator to view its properties and check if Coded UI can recognize it well. Also you can use the SPY++ tool.

    If the adorner decorator can’t be recognized well, I am afraid that you need to write extension to enable CodedUI to recognize it in order to Coded UI Test it.

    Best regards,


    Amanda Zhu [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, December 12, 2012 6:54 AM
    Moderator
  • Hello WPFASL,

    What about your issue now? Could you get useful information from our reply?

    Would you mind letting us know the result of the suggestion?

    Best regards,


    Amanda Zhu [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, December 13, 2012 9:19 AM
    Moderator
  • The adorner decorator above the TextBox is the standard red error border, which is displayed when the textbox is invalidated through binding.

    The crosshair tool in the Coded UI test builder does not find this border, it finds the TextBox below.

    I think, I have to give up on this approach, and instead try to get information about the red border through the adorner layer, and code this into the test.

    Thursday, December 13, 2012 9:31 AM
  • Agreed but getting addressibility to WPF from test case and a running WPF instance is something I haven't figured out yet.  Let me know what you find.

    I know that you can use reflection to creatinstance but that's a lot of hard work in my opinion.  Too bad the Test Framework doesn't have helper class to get us to the native class support easier.


    JP Cowboy Coders Unite!

    Thursday, December 13, 2012 9:55 PM
  • Apparently not even SPY++ can see WPF FrameworkElements which is what the AdornerDecorator is.  That's why I suggested trying to get addressibility to WPF side as that's the only way you'll find it from code behind.  NOTE: You may have to resort to walking the Visual Tree to get it.

    This is a gap in capability of CodedUI tests... No doubt.


    JP Cowboy Coders Unite!

    Thursday, December 13, 2012 9:59 PM
  • As I mention in my first post, I am new to Coded UI test and is not aware of difficulties concerning getting lower level access to WPF/control and use this in test.

    But using the VisualTreeHelper seems to be a good way to proceed.

    Friday, December 14, 2012 10:42 AM
  • Hello WPFASL,

    What about your issue now? Can you resolve the issue using VisualTreeHelper?

    I am marking your reply as answer. If possible, you can update the answer after you have resolved the issue with VisualTreeHelper.  If you don't think it is an answer, you can come back and unmark it as answer. If you have anything unclear, please feel free to come back and ask me.

    Best reagrds,


    Amanda Zhu [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, December 18, 2012 12:39 AM
    Moderator
  • I am not sure whether the VisualTreeHelper will solve the problem.

    It can give access to the adorner layer. But this does not give any clues about whether a red marking is drawn above the textbox.

    I have testet the visibility of the adorner layer in case the textbox is without error and in the opposite case. Result is that the adorner layer is visible in both case, and its dimensions equals the entire client area.

     Therefore I am a bit stuck, at this moment.

    Tuesday, December 18, 2012 10:40 AM
  • Hello WPFASL,

    Glad to receive your reply.

    According to your description, it seems that the adorner layer is always visible no matter there are errors with the textbox or not. If yes, I think that you should try to check your application and see whether there is anything wrong with the logic of adorner layer state, if you have set how the adorner layer should display in the application code.

    If I have misunderstood anything, please feel free to let me know.

    Best regards,


    Amanda Zhu [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, December 19, 2012 1:40 AM
    Moderator
  • The application I am using to test this is very simple standard WPF Application. This means that the Client Window includes an adorner decorator by default.

    According to documentation, the adorner decorator creates an adorner layer that applications can use to draw special appearance that appear above controls in the window.

    This is what happens when a control is invalidated through binding.

    On the client window, I have placed a TextBox, which is binded to the code by the following lines of code:

            public static void SetTextBoxTextTwoWayValidationBinding(TextBox textBox, MainWindow main, string property)
            {
                Binding binding = new Binding();
                binding.Source = main;
                binding.Path = new PropertyPath(property);
                binding.Mode = BindingMode.TwoWay;
                binding.NotifyOnValidationError = true;
                binding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
                binding.ValidatesOnDataErrors = true;
                textBox.SetBinding(TextBox.TextProperty, binding);
            }

    Here is the the property handler used for testing:

            public string Text
            {
                get
                {
                    return text;
                }
    
                set
                {
                    text = value;
    
                    AdornerLayer al = AdornerLayer.GetAdornerLayer(textBox1);
    
                    if (al != null)
                    {
                        Visibility visibility = al.Visibility;
    
                        int k = VisualTreeHelper.GetChildrenCount(al);
                    }
    
                    OnPropertyChanged("Text");
                }
            }

    The test I did shows that the adorner is always visible, both when the control is invalidated through IDataErrorInfo and when valid text is entered.

    By debugging I can see that the adorner layer fills the entire client area.

    From this I conclude, that I do not interact with a standard mechanism used by WPF-controls to draw error state in the adorner layer.

    Unfortunately, it seems to me that I can do nothing to set up a test for this. Except to test that the adorner layer is present and that the Validation.Error property is set to true.

    As I see it, this is what I learned from this discussion.

    Wednesday, December 19, 2012 9:43 AM