WPF 使用 FlaUI3 实现 UI 自动化测试,并解决 System.IO.FileNotFoundException: Could not load file or assembly 'Accessibility, Version=4.0.0.0, Culture=neutral, PublicKeyToken=XXXXXXXX'.

参考

环境

软件/系统 版本 说明
Windows windows 10 专业版 22H2 64 位操作系统, 基于 x64 的处理器
Microsoft Visual Studio Community 2022 (64 位) - Current 版本 17.14.9
.NET 6
FlaUI.UIA3 5.0.0 nuget依赖库

解决 System.IO.FileNotFoundException: Could not load file or assembly 'Accessibility, Version=4.0.0.0, Culture=neutral, PublicKeyToken=XXXXXXXX'.

这个问题应该是 .NET6+ 支持跨平台问题导致的,所以运行 FlaUI 需要在 .NET6+ 上面指定运行的平台为 Windows

  1. 解决方案资源管理器->对应项目->右键->属性
    image
  2. 应用程序->常规->目标OS 设置为 Windows。(目标操作系统版本是在选择目标OS后自动带出的配置)
    image

正文

FlaUI 是一个 .NET 库,有助于对 Windows 应用程序(Win32、WinForms、WPF、Store Apps 等)进行自动化 UI 测试。

安装 FlaUI.UIA3 即可,对应版本的 FlaUI.Core 会自动安装。

代码

using FlaUI.Core;
using FlaUI.Core.AutomationElements;
using FlaUI.Core.Definitions;
using FlaUI.Core.Input;
using FlaUI.Core.Tools;
using FlaUI.UIA3;
using System.Diagnostics;

namespace UITest
{
    [TestClass]
    public sealed class Test1
    {
        [TestMethod]
        public void TestMethod1()
        {
            // 启动应用程序
			var app = Application.Launch(Path.Combine(Environment.CurrentDirectory,
			"..", "..", "..", "..",
			"WPFNetConnect", "bin", "Debug", "net6.0-windows", "WPFNetConnect.exe"));

            // 使用 UIA3 自动化模式
            using (var automation = new UIA3Automation())
            {
                // 获取主窗口
                var mainWindow = app.GetMainWindow(automation);

                if (mainWindow == null)
                {
                    Assert.Fail("主窗口未找到");
                }

                // 定位账号输入框
                var accountTextBox = mainWindow.FindFirstDescendant(cf =>
                    //cf
                    cf.ByControlType(ControlType.Edit).And(cf.ByAutomationId("LoginAccountTextBox"))
                )?.AsTextBox();

                if (accountTextBox == null)
                {
                    Assert.Fail("账号输入框未找到");
                }
                accountTextBox.Enter("admin");

                // 定位密码框(使用自动化ID)
                var passwordBox = mainWindow.FindFirstDescendant(cf =>
                    cf.ByAutomationId("LoginPasswordBox")
                );

                if (passwordBox == null)
                {
                    Assert.Fail("密码框未找到");
                }
                passwordBox.Focus();
                Keyboard.Type("123456");

                // 定位登录按钮(通过按钮文本)
                var loginButton = mainWindow.FindFirstDescendant(cf =>
                    cf.ByControlType(ControlType.Button).And(
                    cf.ByName("登录"))
                )?.AsButton();

                if (loginButton == null)
                {
                    Assert.Fail("登录按钮未找到");
                }
                loginButton.Invoke();
            }
            app.Close();
        }
    }
}

结果

image

 TestMethod1
   源: Test1.cs 行 15
   持续时间: 1.1 秒
posted @ 2025-07-17 13:17  夏秋初  阅读(77)  评论(0)    收藏  举报