Skip to content
Tom Austin edited this page Jul 12, 2020 · 5 revisions

Getting Started

Installing SHAutomation

SHAutomation is distributed as a Nuget package, a link to the package page can be found here. Install by either using the Nuget package manager within Visual Studio or through the package manager console.

Install-Package SHAutomation

Once installed add a using for SHAutomation.UIA3 e.g.

using SHAutomation.UIA3;

Starting an application

SHAutomation supports starting Windows applications as well as Windows Store apps. There are a few different ways to start applications and these methods are documented below.

EXE Path

This is the easiest way to start an application using SHAutomation, pass the full path to your EXE to the Launch method

using SHAutomation.UIA3;

using var application = Application.Launch("C:\\ApplicationPath\\Application.exe");
using var automation = new UIA3Automation();
{
    var window = application.GetMainWindow(automation);
    var element = window.Find("elementAutomationId");
}

ProcessStartInfo

If you need to start your application using specific command parameters this is the easiest way to do it.

using SHAutomation.UIA3;

using var application = Application.Launch(new ProcessStartInfo("C:\\ApplicationPath\\Application.exe", "-aut"));
using var automation = new UIA3Automation();
{
    var window = application.GetMainWindow(automation);
    var element = window.Find("elementAutomationId");
}

Windows Store Application

To attach to a Windows Store Application use LaunchStoreApp

using SHAutomation.UIA3;

using var application = Application.LaunchStoreApp("Microsoft.WindowsCalculator_8wekyb3d8bbwe!App");
using var automation = new UIA3Automation();
{
    var window = application.GetMainWindow(automation);
}

Attaching to an existing application

If the application you wish to interact with is already running then use the Attach method. This is called in a very similar way to Launch by either passing a path to the EXE or finding the process and passing it to Attach.

using SHAutomation.UIA3;

var process = Process.GetProcesses().Where(x => x.ProcessName == "ApplicationName").First();
using var application = Application.Attach(process);
using var automation = new UIA3Automation();
{
    var window = application.GetMainWindow(automation);
    var element = window.Find("elementAutomationId");
}

AttachOrLaunch

If you are unsure if your application is already running you can use AttachOrLaunch. This method works exactly the same as Attach however if the application is not running it will be started.

using SHAutomation.UIA3;

var process = Process.GetProcesses().Where(x => x.ProcessName == "ApplicationName").First();
using var application = Application.AttachOrLaunch(process);
using var automation = new UIA3Automation();
{
    var window = application.GetMainWindow(automation);
    var element = window.Find("elementAutomationId");
}