FlaUI 自动化

环境:

net8.0

WinForm

Nuget包:FlaUI.Core、FlaUI.UIA3

FlaUI 是一个 .NET 库,用于辅助 Windows 应用程序(Win32、WinForms、WPF、应用商店应用等)的自动化 UI 测试。

1、简单示例:

打开notepad++,打印当前窗口标题

cs 复制代码
 // 启动应用,程序没有启动就启动它,已经启动就 Attach 到它
            string exePath = @"C:\Program Files\Notepad++\notepad++.exe";
            FlaUI.Core.Application app = null;
            while (app == null)
            {
                try
                {
                    // 尝试 Attach 已启动进程
                    app = FlaUI.Core.Application.Attach("notepad++");
                }
                catch
                {
                    // 没有启动就 Launch
                    try
                    {
                        app = FlaUI.Core.Application.Launch(exePath);
                    }
                    catch
                    {
                        // 程序还没准备好
                        Thread.Sleep(1000);
                    }
                }
            }

            using var automation = new UIA3Automation();
            var mainWindow = app.GetMainWindow(automation);
            Console.WriteLine("窗口标题:" + mainWindow.Title);

3、复杂示例

启动TextDriverSimulator 程序,查找btnListen 按钮点击,监听rtxbLog文本框内容,点击btnSendALUCommand按钮

cs 复制代码
 string textDriverSimulatorPath = @"C:\EFEM-Simulation\Debug\TextDriverSimulator.exe";
            var textDriverSimulatorApp = FlaUI.Core.Application.Launch(textDriverSimulatorPath);
            FlaUI.Core.AutomationElements.Window textDriverSimulatorWindow = null;
            using var automation = new UIA3Automation();

            textDriverSimulatorWindow = (Retry.WhileNull<FlaUI.Core.AutomationElements.Window>(() =>
            {
                return textDriverSimulatorApp.GetMainWindow(automation);
            }, timeout: TimeSpan.FromSeconds(10)
            )).Result;//等待窗口

            // var btnListen = textDriverSimulatorWindow.FindFirstDescendant(cf =>
            //cf.ByAutomationId("btnListen"))?
            //.AsButton();
            // btnListen?.Click();//获取 btnListen 控件

            var btnListen = (Retry.WhileNull<FlaUI.Core.AutomationElements.Button>(() =>
                textDriverSimulatorWindow.FindFirstDescendant(cf => cf.ByAutomationId("btnListen")).AsButton(),
                TimeSpan.FromSeconds(5)
            )).Result;//等待 btnListen 控件出现
            btnListen?.Click();

            var rtxbLog = (Retry.WhileNull<FlaUI.Core.AutomationElements.TextBox>(() =>
                textDriverSimulatorWindow.FindFirstDescendant(cf => cf.ByAutomationId("rtxbLog"))
                                    ?.AsTextBox(), TimeSpan.FromSeconds(5)
           )).Result;//等待 rtxbLog 控件出现
            bool connectedSuccess = false;
            while (!connectedSuccess)
            {
                connectedSuccess = rtxbLog.Text.Contains("AcceptTcpClient Connected");
            }//监听RichTextBox 内容,直到出现"AcceptTcpClient Connected"
            var sendBtn = (Retry.WhileNull<FlaUI.Core.AutomationElements.Button>(() =>
              textDriverSimulatorWindow.FindFirstDescendant(cf => cf.ByAutomationId("btnSendALUCommand")).AsButton(),
              TimeSpan.FromSeconds(5)
          )).Result;//等待 btnListen 控件出现
            sendBtn?.Click();

2、查看应用程序的 UI 元素树,属性和模式 FlaUInspect

4、代码示例

https://github.com/czjnoe/FlaUISamplehttps://github.com/czjnoe/FlaUISample

相关推荐
7177779 小时前
厘清 Gitee Test 能力边界:测试管理、自动化与 DevSecOps 协同路径
运维·gitee·自动化
朗宇芯工控10 小时前
工厂 “钢铁焊工”:焊接机器人的技术逻辑与发展方向
机器人·自动化·制造·工业·运动控制系统
代码方舟11 小时前
零信任架构实战:基于天远企业年报信息核验构建自动化高并发尽调网关
运维·人工智能·架构·自动化
zhonyu鱼12 小时前
Home Assistant:开源的智能家居自动化平台,统一控制全屋设备
开源·自动化·智能家居
2301_7867566013 小时前
不做分子仿真、不布局自动化实验室,垂直科研智能平台同样跑通 AI for Science 可持续变现
运维·人工智能·自动化
志栋智能13 小时前
超自动化安全如何提升合规与审计效率?
运维·安全·自动化
小张同学a.14 小时前
Ansible自动化运维实战3——变量分离、Roles 角色开发与 Git 托管分发实战
运维·服务器·自动化·ansible·playbook·role
酷可达拉斯14 小时前
自动化运维-Ansible Role详解
linux·运维·服务器·自动化·ansible
电化学仪器白超15 小时前
MV-CS200-10UC相机参数配置
python·单片机·嵌入式硬件·数码相机·自动化·ltspice
微硬创新15 小时前
不用换设备,也能接入智能系统:耐达讯自动化NY-B801网关的神奇功效
人工智能·网络协议·自动化·信息与通信