UI Desk Automation tool to automate tasks on Windows


UIDeskAutomation.Automation3.0

You can use UIDeskAutomation.Automation3.0 library to ease your .NET development with Microsoft Windows Automation API 3.0 (the unmanaged version of UI Automation API which is located in Windows\System32\UIAutomationCore.dll, was introduced starting with Windows 7 and was written as a Component Object Model - COM library) or to create automated tasks in Windows.

To use this library with C# you just need to add UIDeskAutomation.dll to the project references as an usual .NET assembly.
Here are some code examples. To start an application you can do the following:

using UIDeskAutomationLib;
...
var engine = new Engine();
engine.StartProcess("calc.exe");

To get the main window of the application you opened do the following:

using UIDeskAutomationLib;
...
var engine = new Engine();
engine.StartProcess("calc.exe");
Window window = engine.GetTopLevel(null, "Calculator");

The first parameter of GetTopLevel method is the window class name. Specify "null" if you want to ignore the class name. The second parameter is the window title. 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:

var windows = engine.GetTopLevelWindows(null, "Calculator");
foreach (Window window in windows)
{
    window.Close();
}

To get a button on the window use the following:

var engine = new Engine();
engine.StartProcess("calc.exe");
Window window = engine.GetTopLevel(null, "Calculator");
var btnOne = window.Button("One", true); // get "1" button: "One" on Windows 10, "1" on Windows 7

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)[1]; to get the same result which is the second button named "One".

To make sure the library find the right element you can highlight it like this: btnOne.Highlight();
To push the button call Invoke method: btnOne.Invoke();

UIDeskAutomationSpy.Automation3.0 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 have this long descriptor:
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 IUIAutomationElement using the InnerElement property:
IUIAutomationElement element = btnOne.InnerElement;
The InnerElement property is available on every type of element.
You can create an object belonging to this library from an IUIAutomationElement using the constructor of each type.

The tool that helps you find the name of elements is UIDeskAutomationSpy.Automation3.0 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 first open it. You can use the left pane of the help to access the contents of the file. As an alternative to the help file, for a complete reference, you can also check THE ONLINE HELP.

A new added feature is the CaptureToFile method available on every type of UI element and it captures the visual element and saves the image in a specified file using an usual image extension (.bmp, .jpg, .jpeg or .png).

© 2020 dDelta Solutions.
www.000webhost.com