How do I check a box using Microsoft uiautomation?

I need to automate the madvr gui and enable smooth movement with uiautomation.
But I can't find recent and simple working samples.

Madvr window identification:

Madvr smooth motion flag identification:

My actual code:

using System;
using System.Diagnostics;

namespace MadAutomation
{
    class Program
    {
        public static void Main(string[] args)
        {
            Console.WriteLine("Enabling the madvr smooth motion feature...");

            EnableSmoothMotion();

            Console.Write("Press any key to continue...");
            Console.ReadKey(true);
        }

        public static void EnableSmoothMotion()
        {
            // Run madvr configuration.         
            Process p = new Process();
            p.StartInfo.FileName = @"C:\Users\Admin\Desktop\madVR\madHcCtrl.exe";
            p.StartInfo.Arguments = "editLocalSettingsDontWait";
            p.Start();

            // Enable smooth motion checkbox.
        }
    }
}

      

+3


source to share


1 answer


Here is a program that does it. Note that you must use the UIAComWrapper NuGet package (written by a Microsoft man) instead of the standard UIAutomation * .dll provided outside the -box for it to work. UIA.NET standard assemblies do not see all AutomationElements, do not know all properties (Aria, etc.).

Yes, that means they are bugging / deprecated , and for some reason Microsoft does not officially ship new versions with newer .NET or Windows versions ... Anyway, UIAComWrapper is a 100% source-compatible replacement, so it is not should be a problem.

If you want to see the differences between the results obtained by UIAComWrapper vs UIA asssemblies, then the same differences you will see if you use Inspect (the official UIA tool) versus UISpy (the legacy official user interface tool), respectively. They look similar, but in reality they are completely different in detail or structure.



public static void EnableSmoothMotion()
{
    bool finished = false;
    // wait for the settings window to show
    Automation.AddAutomationEventHandler(WindowPattern.WindowOpenedEvent, AutomationElement.RootElement, TreeScope.Children, (sender, e) =>
    {
        var window = (AutomationElement)sender;
        if (window.Current.ClassName != "TFMadVRSettings")
            return;

        // get the tree element
        var tree = window.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Tree));

        // get the smooth motion element & select it
        var smoothMotion = tree.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.NameProperty, "smooth motion"));
        ((SelectionItemPattern)smoothMotion.GetCurrentPattern(SelectionItemPattern.Pattern)).Select();

        // get the tab element
        var tab = window.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Tab));

        // get the pane element
        var pane = tab.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Pane));

        // get the first checkbox & ensure it clicked
        var cb = pane.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.CheckBox));
        TogglePattern tp = (TogglePattern)cb.GetCurrentPattern(TogglePattern.Pattern);
        if (tp.Current.ToggleState != ToggleState.On) // not on? click it
        {
            ((InvokePattern)cb.GetCurrentPattern(InvokePattern.Pattern)).Invoke();
        }

        // NOTE: uncomment the two following line if you want to close the window directly
        // get the ok button & push it
        //var ok = window.FindFirst(TreeScope.Children, new AndCondition(
        //    new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Button),
        //    new PropertyCondition(AutomationElement.NameProperty, "OK")));
        //((InvokePattern)ok.GetCurrentPattern(InvokePattern.Pattern)).Invoke();

        finished = true;
    });

    // run the program
    Process p = new Process();
    p.StartInfo.FileName = @"C:\Users\Admin\Desktop\madVR\madHcCtrl.exe";
    p.StartInfo.Arguments = "editLocalSettingsDontWait";
    p.Start();

    while(!finished)
    {
        Thread.Sleep(100);
    }
    Automation.RemoveAllEventHandlers();
}

      

Note. I used an event handler here instead of process.MainWindowHandle because the main process window is not a settings window and the settings window is not a child of the main window.

+5


source







All Articles