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

相关推荐
此冬歌咏17 小时前
自动化服务器运维监控系统(python+shell)
linux·运维·服务器·开发语言·python·自动化
自动化监测Learner18 小时前
自动化监测数据传不出廊道?大坝通信组网选型实战:RS485、光纤、4G、LoRa、北斗短报文一篇讲透
运维·网络·自动化
HAHAXX818 小时前
2026智能自动化落地:通义灵码与Cursor加持,RPA融合生成式AI的工程化实践
人工智能·自动化·rpa
chuntian_tester18 小时前
AI自动化第1步【系统探索】
人工智能·测试工具·ai·自动化
可乐鸡翅yeah_18 小时前
HLS 自动化拨测与手动调试分工,流媒体线上监控体系建设实践
运维·自动化·测试用例·音视频·媒体·m3u8·音视频在线播放
PFFstronger18 小时前
从 0 到 1 搭建接口自动化测试框架:分层架构 + 数据驱动 + 接口依赖编排
python·架构·自动化
weixin_4407305019 小时前
playwright浏览器自动化实战笔记3-登陆以及退出登陆流程-多用户操作
笔记·python·自动化
吴佳浩 Alben20 小时前
构建企业级 DevOps 排错 Agent:从日志告警到自动化修复 PR
大数据·人工智能·语言模型·架构·自动化·ai编程·devops
poiu123465721 小时前
自动化任务处理技术选型分析:三款工具特征对比
自动化
皮卡丘不断更21 小时前
视频自动剪、内容自动发:我的多平台营销自动化工作台
大数据·运维·自动化·视频剪辑·营销自动化·多平台运营·creatorhub