4

I have just started using AutomationElement because we want to do integration testing of our custom controls, and I think I should be using AutomationElement.

I have successfully created a Window with a custom control in it, and can successfully obtain AutomationElements for both the window and control

  // Retrieve the View
  System.Windows.Automation.Condition viewCondition = new PropertyCondition(AutomationElement.AutomationIdProperty, "MyTestView");
  AutomationElement view = AutomationElement.RootElement.FindFirst(TreeScope.Children, viewCondition);
  Assert.IsNotNull(view);

  // Retrieve the CustomControl
  System.Windows.Automation.Condition comboboxCondition = new PropertyCondition(AutomationElement.AutomationIdProperty, "MyCustomControl");
  AutomationElement combobox = view.FindFirst(TreeScope.Children, comboboxCondition);
  Assert.IsNotNull(comboboxCondition);

Now, what I want to do is use, for example the ValuePattern. And this is where I become confused.

Looking for information, I searched the WPF source at referencesource.microsoft.com. I encountered ComboboxAutomationPeer, which implements IValueProvider, so now I'm confused.

Should I also implement MyCustomControlAutomationPeer that implements IValueProvider, and will AutomationElement then work with ValuePattern? Or should I have MyCustomControl implement IValueProvider?

Simon Mourier
  • 132,049
  • 21
  • 248
  • 298
Diana
  • 789
  • 8
  • 34
  • 2
    An AutomationPeer exists in the app you are trying to automate. Like this one for a ComboBox in a WPF app. An AutomationElement is one you use in the code that *uses* automation to tinker with such a WPF app. This code. – Hans Passant Jun 26 '15 at 16:59
  • Thank you for explaining the distinction Hans. – Diana Jun 27 '15 at 08:28

1 Answers1

1

You don't have to implement anything to use a pattern. UI Automation does this for you (acting as a proxy for the target application). This is well explained in official documentation here: Get Supported UI Automation Control Patterns

Here is an example extract:

    SelectionItemPattern pattern;
    try
    {
        pattern = yourAutomationElement.GetCurrentPattern(SelectionItemPattern.Pattern) as SelectionItemPattern;
    }
    catch (InvalidOperationException ex)
    {
        Console.WriteLine(ex.Message);  // Most likely "Pattern not supported." 
        return;
    }
    pattern.Select();
Simon Mourier
  • 132,049
  • 21
  • 248
  • 298
  • Ok, I'm gonna be more specific now: Suppose I have a custom control made up of template parts. You're saying this custom control will automagically have the Patterns of its parts? I.e. I don't have to implement anything in my custom control to do UI Automation, because it will inherit the patterns of, say, the textbox part? – Diana Jun 27 '15 at 07:24
  • Ok, so, now you're talking server side where AutomationPeers are used (my sample is client side). For server side, *in general*, you don't have to do anything as WPF builds peers automatically for standard controls. However if you need to add some custom behavior (for example, if you have a pure drawing but you want to highlight some parts), then you can. But you should check what your controls expose out-of-the-box first using UIA sdk tools (inspect.exe, etc.) – Simon Mourier Jun 27 '15 at 07:47
  • I do not have a standard control. I have a custom control with template parts. Does my custom control inherit the patterns of the template parts? – Diana Jun 27 '15 at 08:26
  • 1
    Yes. But like I said, just try it and use Inspect from the SDK, you will see if it's seen and how, what patterns are supported, etc. – Simon Mourier Jun 27 '15 at 16:31
  • Thank kindly for your patient replies, I'm back at work on Monday and will continue my experiments with AutomationElement and ValuePattern. (Will accept your answer then) – Diana Jun 27 '15 at 20:11
  • Haven't gotten around to experiment with this further, sorry. This hasn't reached the top of our sprint plan lately.. Will return to this as soon as I can. – Diana Jul 13 '15 at 18:06