Windows应用的 AI 辅助测试

借助 winapp ui 命令和 winui-ui-testing 技能,AI 代理可以自动检查、交互并验证应用的 UI,并以自然语言描述它所看到的内容,让你无需先编写测试支撑代码即可进行迭代。

UI 检查命令

winui-ui-testing 技能包含在 winui@awesome-copilot 插件中。 如果在 安装过程中安装了插件,则无需进行其他安装。

Command 它的作用是什么
winapp ui inspect 转储当前运行的应用的辅助功能树
winapp ui screenshot 截取应用窗口截图
winapp ui click <selector> 单击按钮或选择项目
winapp ui invoke <selector> 对控件调用默认操作
winapp ui search <text> 按名称或 AutomationId 在元素树中搜索
winapp ui set-value <selector> <value> 设置 TextBox 或 ComboBox 的值

要求代理测试应用

  1. 运行应用:
    dotnet run
    
  2. 向代理询问自然语言问题,例如:

    “查看正在运行的应用,并告诉我当窗体为空时是否可以访问”保存“按钮。

  3. 代理使用 winapp ui inspectwinapp ui screenshot 检查应用并做出响应。

通过此工作流,可以在编写一行测试代码之前捕获问题(缺少键盘焦点、禁用的控件、布局问题)。

编写自动测试

以交互方式浏览应用后,请让代理生成正式测试。 使用此起始提示词:

Write an xUnit UI test for my WinUI 3 app that:
1. Launches the app with dotnet run
2. Verifies the main window title is "My App"
3. Clicks the button with AutomationId "SaveButton"
4. Verifies a success message appears
Use the winapp ui commands for element interaction.

生成的测试使用 winapp ui 命令作为交互层,因此它们无需单独的 UI 自动化框架即可运行。

在 XAML 中设置 AutomationId

若要 winapp ui click 可靠地定位元素,请在 XAML 中设置 AutomationProperties.AutomationId

<Button AutomationProperties.AutomationId="SaveButton"
        Content="Save" />
<TextBox AutomationProperties.AutomationId="TitleInput" />

询问代理: “将 AutomationId 属性添加到此 XAML 中的所有交互式控件。

在 CI 中运行测试

可以在GitHub Actions工作流中运行winapp ui命令。 测试需要图形界面会话;请使用带有 runs-on: windows-latest 的 Windows 运行器。

name: UI tests
on: [push, pull_request]
jobs:
  ui-test:
    runs-on: windows-latest
    steps:
      - uses: actions/checkout@v4
      - name: Setup .NET
        uses: actions/setup-dotnet@v4
        with:
          dotnet-version: '9.0.x'
      - name: Install winapp CLI
        run: winget install --id Microsoft.WinAppCLI --silent --accept-package-agreements --accept-source-agreements --disable-interactivity
      - name: Build
        run: dotnet build
      - name: Run UI tests
        run: dotnet test --filter Category=UITest

注释

winapp ui 命令与正在运行的进程的实时辅助功能树进行交互。 测试必须在测试设置阶段(或在 TestInitialize 步骤中)启动该应用,并在测试结束时将其关闭。

自动检查无障碍功能

winapp ui inspect 命令输出完整的无障碍树,包括元素名称、类型、状态和 AutomationId。 请让您的客服代表审查一下,看是否存在无障碍方面的不足:

Run "winapp ui inspect" on the running app and identify any issues:
- Controls without accessible names
- Missing keyboard focus order
- Buttons or links that aren't reachable by Tab
Report findings as a prioritized list, most critical first.

在运行正式审计工具之前,这个提示很适合用来进行初步的无障碍审查。 有关完整的辅助功能清单,请参阅Windows应用的辅助功能概述