【问题标题】:How to check a checkbox with microsoft uiautomation?如何使用 microsoft uiautomation 检查复选框?
【发布时间】:2015-06-17 01:10:49
【问题描述】:

我需要自动化 madvr gui 并使用 uiautomation 启用平滑运动。
但找不到最近的简单工作示例。

madvr窗口识别:

madvr 平滑运动复选框识别:

我的实际代码:

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.
        }
    }
}

【问题讨论】:

    标签: c# ui-automation microsoft-ui-automation


    【解决方案1】:

    这是一个执行此操作的程序。请注意,您必须使用 NuGet UIAComWrapper 包(由 Microsoft 人员编写),而不是提供开箱即用的标准 UIAutomation*.dll 以使其工作。标准的 UIA .NET 程序集看不到所有的 AutomationElement,不知道所有的属性(Aria 等)。

    是的,这意味着它们是bugged/obsolete,并且由于某种原因,Microsoft 没有正式发布带有新的 .NET 或 Windows 版本的更新...无论如何 UIAComWrapper 是 100% 来源- 兼容的直接替换,所以这应该不是问题。

    如果您想查看 UIAComWrapper 与 UIA 程序集返回的内容之间的差异,您会看到使用 Inspect(官方 UIA 工具)与 UISpy(过时的官方 UI 工具)。它们看起来相似,但实际上在细节或结构上大不相同。

    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's 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();
    }
    

    注意:我在这里使用事件处理程序而不是 process.MainWindowHandle 因为进程的主窗口不是设置窗口,设置窗口不是主窗口的子窗口。

    【讨论】:

    • 太棒了!谢谢!您写过关于 Inspect 的文章,那么 Visual UIA 验证呢?它有什么问题吗?这是我使用的软件,你在我的屏幕截图中看到了。
    • 是的,VisualUIAVerifyNative 实际上类似于inspect,但它是.net(inspect 是本机的),并且确实使用了我提到的UIAComWrapper。
    • 您使用InvokePattern.Invoke()而不是TogglePattern.Toggle()的任何特殊原因?
    • @HarryJohnston - 嗯...不,元素可能支持不止一种模式。我可能只是使用了最通用的一个:-)
    猜你喜欢
    • 2023-03-26
    • 1970-01-01
    • 2020-06-07
    • 1970-01-01
    • 2011-10-14
    • 2014-05-13
    • 1970-01-01
    • 2011-12-22
    相关资源
    最近更新 更多