Thierry
Thierry

Reputation: 6458

automationElement.RootElement.FindFirst always returns null

I have a requirement that may need to use something like UI Automation so i'm trying to learn the basics but I'm not having much luck.

I found this line of code in a couple of examples from different articles:

var calcWindow = AutomationElement.RootElement.FindFirst(TreeScope.Children,
new PropertyCondition(AutomationElement.NameProperty, "Calculator"));

but whenever I run this line, calcWindow is null. I've noticed that it will be set to a value when I run 2 instances of the calculator.

The same problem applies to RootElement.FindAll where it returns null when a single instance of the calculator runs but when I run 2 instances, it will return a value and its .Count() property is set to 1.

Any ideas?

Thanks.

UPDATE 1

I probably should have included the articles I was looking at:

Note that I'm running these tests using .NET 2015 on a Windows 10 Pro 64-bit (os build: 17134.112 - version: 1803).

Upvotes: 1

Views: 1516

Answers (1)

Max Young
Max Young

Reputation: 1662

In windows 10 the calculator is a UWP app so you need to use a different method to find it.

Here is an example of how FlaUI is getting the new calculator vs the legacy calculator. FlaUI is written on top of both the managed UIAv2 API and the unmanaged UIAv3 COM wrapper.

    protected override Application StartApplication()
    {
        if (OperatingSystem.IsWindows10())
        {
            // Use the store application on those systems
            return Application.LaunchStoreApp("Microsoft.WindowsCalculator_8wekyb3d8bbwe!App");
        }
        if (OperatingSystem.IsWindowsServer2016())
        {
            // The calc.exe on this system is just a stub which launches win32calc.exe
            return Application.Launch("win32calc.exe");
        }
        return Application.Launch("calc.exe");
    }

Upvotes: 1

Related Questions