Tuesday, June 10, 2014

Microsoft UI Automation Framework

Microsoft UI Automation is the new accessibility framework for Microsoft Windows used to provide programmatic access to UI elements on the desktop. It can be used to automate Windows applications, Web applications and Windows Store apps

To inspect the automation element properties, you can use the accessibility tools that ship with Windows SDK http://blogs.msdn.com/b/seealso/archive/2010/07/22/where-did-the-accessibility-tools-go.aspx
 

 
Pre-Requisite
Add a reference to UIAutomationClient.dll and UIAutomationTypes.dll and include the namespace
using System.Windows.Automation;

Desktop or Root element
The root element is desktop which can be obtained by
AutomationElement desktop = AutomationElement.RootElement;

 Finding the Application under Test

The application under test can be found by looking for the children of desktop
AutomationElement applicationUnderTest = desktop.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.NameProperty, "ApplicationName"));

Finding Child elements of Application under Test

AutomationElement child = applicationUnderTest.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.NameProperty, "ChildName"));

Finding Descendants of Application under Test

AutomationElement child = applicationUnderTest.FindFirst(TreeScope. Descendants, new PropertyCondition(AutomationElement.NameProperty, "DescendentName"));

Various ways of finding an Element

AutomationElement.NameProperty
AutomationElement.AutomationIdProperty
AutomationElement.ClassNameProperty
AutomationElement.FromHandle(IntPtr hwnd)
AutomationElement.ControlTypeProperty
Etc.

Entering Text in an Element

AutomationElement textbox = applicationUnderTest.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.NameProperty, "TextboxName"));
object textboxValuePattern = null;
if (textbox.TryGetCurrentPattern(ValuePattern.Pattern, out textboxValuePattern))
{
   ((ValuePattern) textboxValuePattern.SetValue("Desired text");
}

Clicking a button

AutomationElement button = applicationUnderTest.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.NameProperty, "ButtonName"));
object t buttonPattern = null;
if (button.TryGetCurrentPattern(InvokePattern.Pattern, out buttonPattern))
{
   ((InvokePattern) buttonPattern.Invoke();
}

Finding the Application under Test using Pointer to a Handle

Sometimes it’s difficult to find an application just by looking at the children of desktop. In these case, it can be found using pointer to its handle

First, create a class which uses Windows USER (User32.dll) component of Windows Operating System
public static class Win32
    {
        [DllImport("user32.dll")]
        public static extern IntPtr FindWindow(String sClassName, String sAppName);
    }

Then get a handle of the application window. You will have to include the namespace
using interop.UIAutomationCore;
IntPtr handle = Win32.FindWindow("Windows.UI.Core.CoreWindow", " ApplicationName ");

Now, you can find the application under test
AutomationElement applicationUnderTest = AutomationElement.FromHandle(handle);

No comments:

Post a Comment