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 小时前
CANN开源项目实战指南:使用oam-tools构建自动化故障诊断与运维可观测性体系
运维·开源·自动化·cann
NPE~5 小时前
自动化工具Drissonpage 保姆级教程(含xpath语法)
运维·后端·爬虫·自动化·网络爬虫·xpath·浏览器自动化
极客小云5 小时前
【ComfyUI API 自动化利器:comfyui_xy Python 库使用详解】
网络·python·自动化·comfyui
不会代码的小测试7 小时前
UI自动化-POM封装
开发语言·python·selenium·自动化
兜兜转转了多少年8 小时前
从脚本到系统:2026 年 AI 代理驱动的 Shell 自动化
运维·人工智能·自动化
L5434144611 小时前
告别代码堆砌匠厂架构让你的系统吞吐量翻倍提升
大数据·人工智能·架构·自动化·rpa
码农阿豪12 小时前
多服务器批量指令管理:从Xshell到自动化运维
运维·服务器·自动化
2501_9419820512 小时前
别再手动发群消息了!企业微信外部群自动化推送的架构设计与实现
运维·自动化·企业微信
Wpa.wk12 小时前
接口自动化 - 接口鉴权处理常用方法
java·运维·测试工具·自动化·接口自动化
0思必得013 小时前
[Web自动化] Selenium获取元素的子元素
前端·爬虫·selenium·自动化·web自动化