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;
}


The most import rows in WindowsOpenWaiter will we find in the waiter class. It’s here that we register to listen to WindopOpenedEvent. We use the RootElement as a starting point to search from.

UIAeventHandler = new AutomationEventHandler(OnWindowOpenedEvent);
Automation.AddAutomationEventHandler(WindowPattern.WindowOpenedEvent,
AutomationElement.RootElement, TreeScope.Descendants, UIAeventHandler);

To make it easier to work with windows I did create a WindowControl that wrap all the functionality that I need to execute on a window in one class. For example I did create a method to use the little ‘x’ button in the windows title to close the window.

public void CloseWindowByXButton()
{
    // Get the title bar
    PropertyCondition condTitelbar = new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.TitleBar);
    AutomationElement titelbar = element.FindFirst(TreeScope.Descendants, condTitelbar);
 
    // Get the close button
    PropertyCondition condButton = new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Button);
    PropertyCondition condName = new PropertyCondition(AutomationElement.NameProperty, "Close");
    AndCondition andCond = new AndCondition(condButton, condName);
    AutomationElement closeButton = titelbar.FindFirst(TreeScope.Descendants, andCond);
 
    // Invoke the close button
    (closeButton.GetCurrentPattern(InvokePattern.Pattern) as InvokePattern).Invoke();
}

I hope this little example have given you some ideas how you can use event for your automations and also some ideas how you can abstract some of the complexity in UIAutomation away in wrappers.

Download the full project:Calculator2.zip

5.00 avg. rating (94% score) - 2 votes

About Peter Wibeck

Comments

One Response to “Fun with UIAutomation and calc 2 (event handling)”

Trackbacks

Check out what others are saying about this post...
  1. […] 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 […]



Speak Your Mind

Tell us what you're thinking...
and oh, if you want a pic to show with your comment, go get a gravatar!

*