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

相关推荐
金智维科技官方5 天前
智能体,重构企业自动化未来
人工智能·自动化·agent·智能体·数字员工
梦想的旅途25 天前
企业微信API:外部群自动化推送实战指南
大数据·机器人·自动化·企业微信·rpa
xiaoginshuo5 天前
流程自动化从传统RPA升级到AI Agent,如何匹配合适的自动化方案
人工智能·自动化·rpa
W-琑5 天前
软件测试-自动化测试
功能测试·自动化
牛奶咖啡135 天前
DevOps自动化运维实践_使用再生龙对Linux系统进行备份还原
运维·自动化·devops·linux系统的备份还原·linux系统克隆备份·再生龙
Youngchatgpt5 天前
如何在 Excel 中使用 ChatGPT:自动化任务和编写公式
人工智能·chatgpt·自动化·excel
GAOJ_K5 天前
同步带模组稳定运行的关键
人工智能·科技·自动化·制造
电化学仪器白超5 天前
EC20CEHDLG-128-SNNS调试记录
python·单片机·嵌入式硬件·自动化
志栋智能5 天前
安全超自动化:从被动防御到主动响应的革命
运维·网络·数据库·人工智能·安全·web安全·自动化
Gofarlic_OMS5 天前
LS-DYNA许可证全局状态及集群计算资源使用可视化监控大屏
运维·开发语言·算法·matlab·自动化