Hello UIAutomation World with WPF

I am currently trying to figure out how the new UIAutmation works. I tried the "Hello World" approach, i.e. the (almost) simplest possible example wich uses UIAutmation to simply click a WPF Button. The Apps UI looks like this:

<Windowx:Class="UIAutomationTest.Window1"

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

Title="UIAutomationTest"Height="328"Width="334"MouseDown="Window1_MouseDown"

>

<Button

AutomationProperties.AutomationId="Button"

AutomationProperties.Name="Button"

AutomationProperties.ItemType="Button"

Name="Button"

Width="100"

Height="100"

Click="Button1_Click">Button</Button>

</Window>

Its just a simple window with a button inside wich has some AutomationProperties defined. When the client area of the window is clicked I want the Button's click to be Simulated via UIAutmation. I tried it like this in the Window1_MouseDown:

PropertyCondition findWindow =newPropertyCondition(AutomationElement.NameProperty,this.Title);

AutomationElement window =AutomationElement.RootElement.FindFirst(TreeScope.Descendants, findWindow);

PropertyCondition findButton =newPropertyCondition(AutomationElement.AutomationIdProperty,"Button");

AutomationElement button = window.FindFirst(TreeScope.Descendants, findButton);// this returns null :(

InvokePattern pattern = button.GetCurrentPattern(InvokePattern.Pattern)asInvokePattern;

pattern.Invoke();

No matter how I configure thee PropertyCondition findButton, FindFirst()alwaysreturns null. What am I doing wrong here?

While are at it: Do Ihave toset the AutomationProperties for WPF controls to make them Discoverable via UIAutmation? Or will UIAutomation be able to find WPF controls anyway. For example when I ask UIAutmation to find all AutomationElements that are AutomationType.ControlType ControlType.Button, will it find all System.Windows.Controls.Buttons?

Also: Is it possible to simulate a Mouseclick anywhere on a given UIElement? (and trigger the belonging WPF events) ? If so, is there a simple example?

Here you can download the (currently not working) simple example I am talking about (Visual Studio Project for with .NET 3.0 RC1).

Thank your for your help.

[13352 byte] By [bitbonk] at [2008-2-22]
# 1

To begin with, it's not a good idea for an app to try to examine its own UI unless you are maintaining separate threads. I haven't confirmed that this is the problem here, but you might try finding the button through a separate client application.

You can see the button's properties in UISpy. You will see that there are default properties. The AutomationId, for example, is taken from the Name of the control. You generally don't have to override properties.

As for simulating a mouse click, the answer is that there is no generic way to do this. UI Automation is very much built around the concept of control patterns. The effect of a mouse click will vary depending on the functionality of a control. To simulate a mouse click on a button, for example, you call the Invoke method on the InvokePattern. To simulate a click on a scroll bar, you call one of the methods of ScrollPattern.

PeterDonnelly-MSFTUE at 2007-9-3 > top of Msdn Tech,Software Development for Windows Vista,Microsoft UI Automation...
# 2

How would you go about showing a context menu that would typically come up via a right-click?

lukemelia at 2007-9-3 > top of Msdn Tech,Software Development for Windows Vista,Microsoft UI Automation...
# 3
There is no way to do this directly in UI Automation. You could perhaps get the ClickablePoint property of the control and use the Win32 functions WindowFromPoint and SendMessage to send WM_RBUTTONDOWN.
PeterDonnelly-MSFTUE at 2007-9-3 > top of Msdn Tech,Software Development for Windows Vista,Microsoft UI Automation...

Software Development for Windows Vista

Site Classified