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

相关推荐
梦帮科技34 分钟前
OpenClaw 桥接调用 Windows MCP:打造你的 AI 桌面自动化助手
人工智能·windows·自动化
feasibility.1 小时前
AI 编程助手进阶指南:从 Claude Code 到 OpenCode 的工程化经验总结
人工智能·经验分享·设计模式·自动化·agi·skills·opencode
xiaobaibai1532 小时前
营销自动化终极形态:AdAgent 自主闭环工作流全解析
大数据·人工智能·自动化
池央3 小时前
CANN 诊断工具链深度解析:oam-tools 的自动化故障信息收集、软硬件状态快照与 AI Core 错误溯源机制
运维·人工智能·自动化
池央4 小时前
CANN 算子合规性与迁移性:自定义算子设计中的安全边界与属性兼容性
人工智能·自动化·信号处理
乾元4 小时前
终端安全(EDR):用深度学习识别未知勒索软件
运维·人工智能·网络协议·安全·网络安全·自动化·安全架构
麦兜*4 小时前
全面掌握深度学习部署技术:基于TensorRT与Triton Inference Server实现高性能模型推理和自动化Pipeline的企业级落地实践指南
人工智能·深度学习·自动化
zhengfei61118 小时前
自动化快速评估工具
运维·自动化
m_1368719 小时前
n8n 启动时报 EACCES permission denied 的完整排查与修复
自动化·n8n
少云清19 小时前
【金融项目实战】6_接口测试 _Jmeter自动化脚本实现(重点)
jmeter·自动化·金融项目实战