UI Desk Automation tool to automate tasks on Windows
You can use UIDeskAutomation to ease your development with Microsoft UI Automation API (the managed version which is part of .NET Framework - System.Windows.Automation) or to create automated tasks in Windows.
To use this library with C# you just need to add the UIDeskAutomation.dll file to the project references as an usual .NET assembly.
Here are some code examples. To start an application you can do the following:
To get the main window of the application you opened do the following:
The first parameter of GetTopLevel method is the window title. This can be ignored by specifying "null". The second parameter is the window class name. This can also be ignored by specifying "null". GetTopLevel returns the first top-level window that meets the search criteria.
If you want to close all "Calculator" windows opened on your desktop before opening a new one do the following:
To get a button on the window use the following:
window.Button("One", true) returns the first button having the name "One". The method looks in all descendants of the main window.
If you want an array with all buttons no matter the name then do the following: var buttons = window.Buttons();
If you want the second button having the name "One" then do the following: var secondBtnOne = window.ButtonAt("One", 2, true); where 2 is the index of the button. The index is starting with 1 (1 based).
A second option would be: var secondBtnOne = window.Buttons("One", true); to get the same result which is the second button named "One".
To make sure the library finds the right element you can highlight it like this: btnOne.Highlight();
To push the button call Invoke method: btnOne.Invoke();
UIDeskAutomationSpy usually gives you a long descriptor that represents a visual object.
The descriptor is created starting with the most top level visual object and ending with the innermost.
You can skip the middle part of the descriptor by setting the "search through descendants" parameter to true.
For example, if you want to get to the Calculator button in the bottom toolbar on the desktop in Windows 10, instead of the long version:
engine.GetTopLevel("", "Shell_TrayWnd").PaneAt("", 2).Pane("Running applications").Toolbar("Running applications").Button("Calculator")
you can use the short version by specifying searchDescendants parameter:
engine.GetTopLevel("", "Shell_TrayWnd").Button("Calculator", true)
You can drop all the middle of the descriptor or just a part of it. This should also work:
engine.GetTopLevel("", "Shell_TrayWnd").PaneAt("", 2).Button("Calculator", true)
Without setting the searchDescendants parameter to true the Button() method will search only through the first level children and will not search in depth.
When more intermediate objects are specified in descriptor the search is faster.
You have access to the inner MS UI Automation AutomationElement object using the InnerElement property:
AutomationElement element = btnOne.InnerElement;
The InnerElement property is available on every type of element.
You can create an object belonging to this library from an AutomationElement using the constructor of each type.
The tool that helps you find the name of elements is UIDeskAutomationSpy that comes with this software.
For a complete reference and actions you can make on elements, you should read the help file (UIDeskAutomationHelp.chm). Don't forget to uncheck the "Always ask before opening this file" checkbox when you open it. You can use the left pane of the help to access the contents of the file.
As an alternative to the local help file, for a complete reference, you can also check THE ONLINE HELP.