UI automation with accessibility object and win 32 API

In my earlier blogs (Fun with UIAutomation and calc, Fun with UIAutomation and calc 2 (event handling)) I have been talking about how to user uiautomation in .NET. Now it time to dig down bellow uiautomation and go directly to the source WIN 32 API. I have tried to translate the first calculator project to use win 32 API and accessibility object instead. The first thing that we need to-do is to create DllImport statement for user32.dll used for handling more or less all windows UI and oleacc.dll used for accessing the accessibility object. You will find my complete solution in the end of the blog.

An example of DllImport is EnumChildWindows that gives use the possibility to get all child windows to a specific window.

[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool EnumChildWindows(IntPtr parentHandle, Win32Callback callback, IntPtr lParam);

Read more

Fun with UIAutomation and calc 2 (event handling)

Last week, I wrote about UIAutomation and calc. Now it’s time to revisit or little example program and replace the automatic start of calc and replace it a method that will wait for any calc to start and take control over that session. To abstract away the waiting code, I did create a new class called WindowOpenWaiter. Let’s take a look how we can use or new class. The full code to this blog can be found in the bottom.

The first thing we need to-do is to create a new instance of WindowOpenWaiter and give the title to the window that it should look for. Next thing is to call the wait methods with a timeout time in mille seconds so that we will not wait forever. The last thing we do is to get the element that the waiter has found for us and save it in some variable.

AutomationElement calc = null;
using (WindowOpenWaiter waiter = new WindowOpenWaiter("Calculator"))
{
    try
    {
        waiter.Wait(1000 * 10);
    }
    catch (TimeoutException e)
    {
        Console.WriteLine(e.Message);
        return;
    }
 
    calc = waiter.Element;
}

Read more

Fun with UIAutomation and calc

Last night I got a cool idea how to show some basic ideas of UIAutomation. I did decide to-do a UIAutomation example with calc as my test subject. The idea is to give a mathematical expression to the program, which will then use calc to solve the problem and show you the result. The main thing is to show how UIAutomation is working and not to calculate.

The first thing that we need is a method that can find an AutomationElement for us. An AutomationElement is anything that you can see on the screen as buttons and input fields. The method take a root AutomationElement that define where to start to search, a name and a class name for the object that we are looking for. We then create two property conditions based on the name and class name. These two conditions are then combined in an AndCondition. We also have OrCondition and NotCondition to use. The last thing we do is to search for the first AutomationElement that match the condition.

private static AutomationElement GetElement(AutomationElement root, string name, string className)
{
    PropertyCondition condName = new PropertyCondition(AutomationElement.NameProperty, name);
    PropertyCondition condClassName = new PropertyCondition(AutomationElement.ClassNameProperty, className);
    AndCondition cond = new AndCondition(condName, condClassName);
    return root.FindFirst(TreeScope.Descendants, cond);
}

Next method that we need is one that can return a invoke pattern. Invoke pattern is used for example to click on a button. This will be used to be able to click on the different buttons in calc

private static InvokePattern GetInvokePattern(AutomationElement element)
{
    return element.GetCurrentPattern(InvokePattern.Pattern) as InvokePattern;
}

Read more