529f7910a1
问题:富文本(RichTextBox、Chromium/CEF、UWP 文本框)的 UIA Name 往往是通用标题 (实测夹具里是 "RichEdit Control"),真正的内容只在 ValuePattern/TextPattern 里; 而新增监控项时默认 ValueKey 是 Name,于是读回来的是标题而不是内容。 - ControlValueKeys 新增跨来源键「文本内容」:UIA 按 Value → Text → Name 顺序取, MSAA = accValue → accName,Win32 = 窗口标题;三种都取不到才报错并说明各自原因 - 新增/拾取控件时默认 ValueKey 改为「文本内容」(旧条目不受影响,仍按各自保存的键读) - 「文本内容」加进三个来源的可选键列表,监控列表下拉里能直接选 - 夹具新增多行富文本 FixtureRichText(内容只在 Value/Text 里) - 回归新增 ValueKeyTests(键表、Name 兜底、默认键),共 27/27 通过 实测(跨进程,目标为独立进程里的夹具窗口): - 文本框 文本内容="可编辑文本";富文本 文本内容="富文本第一行\r富文本第二行\r"; 按钮(无 Value/Text 模式)自动退回 Name="测试按钮" - 拾取点在两个控件上都精确命中 Edit / Document,且能在窗口树里找到同一节点 - 无界面读取端到端:2 条配置(//Document[...]、//Edit[...] + 文本内容)全部命中并取到值
241 lines
10 KiB
C#
241 lines
10 KiB
C#
// =====================================================================================
|
||
// UiaFixture —— 跨进程验证用的真实 WinForms 夹具(单独的可执行程序)。
|
||
//
|
||
// 为什么必须是独立进程:本工具靠 UIA 读取“别的进程”的控件树,而 UIA 在**同一进程内**
|
||
// 自读自己的控件树会崩溃(见 MEMORY:in-process UIA self-reads segfault)。
|
||
// 因此需要一个真正独立的进程暴露一棵已知且稳定的控件树,让工具跨进程去读、去拾取、
|
||
// 去按 path 定位——这样测的才是生产路径。夹具自身不做任何 UIA 调用。
|
||
//
|
||
// 这棵树的控件类型/名称/AutomationId 都固定,且窗口标题与类名在多次运行间保持不变,
|
||
// 所以保存的监控条目、悬浮条显示条件可以长期复用它。
|
||
//
|
||
// 打开方式:tests\run-fixture.cmd(或用 Rider/VS 直接跑本项目)→ 工具里
|
||
// 刷新窗口 → 选中「UIA 测试夹具」→ 检查选中窗口;验证清单见 tests\README.md。
|
||
// =====================================================================================
|
||
using System;
|
||
using System.Drawing;
|
||
using System.Windows.Forms;
|
||
|
||
namespace UiaFixture
|
||
{
|
||
/// <summary>
|
||
/// A deliberately ordinary window for exercising the tool: every kind of control the tool has to
|
||
/// read (button, text box, checkbox, combo, radio, tree, list, grid, tabs) in one window, with
|
||
/// stable names and AutomationIds so a saved path keeps resolving.
|
||
///
|
||
/// This exists because the PACS/OCS clients either expose nothing (CEF shells) or expose a large
|
||
/// tree nobody wants to hand-verify against; here the expected answer is known.
|
||
///
|
||
/// Run it, then in the tool: 刷新窗口 → select "UIA 测试夹具" → 检查选中窗口. The window title and
|
||
/// class stay the same between runs, so saved monitor entries and floating-bar conditions can be
|
||
/// built against it.
|
||
/// </summary>
|
||
public sealed class FixtureForm : Form
|
||
{
|
||
private readonly Label _status;
|
||
private readonly TextBox _input;
|
||
private readonly RichTextBox _rich;
|
||
private readonly CheckBox _check;
|
||
private readonly ComboBox _combo;
|
||
private readonly TreeView _tree;
|
||
private readonly ListView _list;
|
||
private readonly DataGridView _grid;
|
||
|
||
public FixtureForm()
|
||
{
|
||
Text = "UIA 测试夹具";
|
||
Name = "FixtureForm";
|
||
StartPosition = FormStartPosition.Manual;
|
||
Location = new Point(60, 60);
|
||
Size = new Size(760, 620);
|
||
MinimumSize = new Size(600, 480);
|
||
|
||
var tabs = new TabControl { Dock = DockStyle.Fill, Name = "FixtureTabs" };
|
||
|
||
// ---- page 1: the everyday controls
|
||
var page = new TabPage("控件") { Name = "ControlsPage" };
|
||
var panel = new Panel { Dock = DockStyle.Fill, Padding = new Padding(10) };
|
||
|
||
var button = new Button
|
||
{
|
||
Text = "测试按钮",
|
||
Name = "FixtureButton",
|
||
AutoSize = true,
|
||
Location = new Point(12, 12)
|
||
};
|
||
button.Click += delegate { SetStatus("按钮被点击:" + DateTime.Now.ToString("HH:mm:ss")); };
|
||
|
||
_input = new TextBox
|
||
{
|
||
Name = "FixtureTextBox",
|
||
Text = "可编辑文本",
|
||
Location = new Point(140, 14),
|
||
Width = 220
|
||
};
|
||
_input.TextChanged += delegate { SetStatus("文本框内容已改为:" + _input.Text); };
|
||
|
||
// 多行富文本:内容较长,用来验证"内容在 Name/Value/Text 里"的取值与拾取定位。
|
||
_rich = new RichTextBox
|
||
{
|
||
Name = "FixtureRichText",
|
||
Text = "富文本第一行" + Environment.NewLine + "富文本第二行",
|
||
Location = new Point(380, 14),
|
||
Size = new Size(240, 60)
|
||
};
|
||
|
||
_check = new CheckBox
|
||
{
|
||
Text = "启用选项",
|
||
Name = "FixtureCheckBox",
|
||
Location = new Point(12, 50),
|
||
AutoSize = true
|
||
};
|
||
_check.CheckedChanged += delegate { SetStatus("复选框:" + (_check.Checked ? "选中" : "未选中")); };
|
||
|
||
var radioA = new RadioButton { Text = "选项 A", Name = "FixtureRadioA", Location = new Point(140, 50), AutoSize = true, Checked = true };
|
||
var radioB = new RadioButton { Text = "选项 B", Name = "FixtureRadioB", Location = new Point(240, 50), AutoSize = true };
|
||
radioA.CheckedChanged += delegate { SetStatus("单选:A"); };
|
||
radioB.CheckedChanged += delegate { SetStatus("单选:B"); };
|
||
|
||
_combo = new ComboBox
|
||
{
|
||
Name = "FixtureComboBox",
|
||
DropDownStyle = ComboBoxStyle.DropDownList,
|
||
Location = new Point(12, 80),
|
||
Width = 200
|
||
};
|
||
_combo.Items.AddRange(new object[] { "第一项", "第二项", "第三项" });
|
||
_combo.SelectedIndex = 0;
|
||
_combo.SelectedIndexChanged += delegate { SetStatus("下拉选择:" + _combo.SelectedItem); };
|
||
|
||
var listLabel = new Label { Text = "列表(两列)", AutoSize = true, Location = new Point(12, 116) };
|
||
_list = new ListView
|
||
{
|
||
Name = "FixtureListView",
|
||
View = View.Details,
|
||
FullRowSelect = true,
|
||
Location = new Point(12, 138),
|
||
Size = new Size(320, 150)
|
||
};
|
||
_list.Columns.Add("姓名", 120);
|
||
_list.Columns.Add("编号", 100);
|
||
for (int index = 1; index <= 3; index++)
|
||
{
|
||
_list.Items.Add(new ListViewItem(new[] { "项目 " + index, "ID" + index }));
|
||
}
|
||
|
||
_tree = new TreeView
|
||
{
|
||
Name = "FixtureTreeView",
|
||
Location = new Point(348, 138),
|
||
Size = new Size(360, 150)
|
||
};
|
||
TreeNode root = _tree.Nodes.Add("根节点");
|
||
root.Nodes.Add("子节点 1");
|
||
root.Nodes.Add("子节点 2");
|
||
root.ExpandAll();
|
||
|
||
_status = new Label
|
||
{
|
||
Name = "FixtureStatusLabel",
|
||
Text = "还没有操作。",
|
||
AutoSize = false,
|
||
Height = 22,
|
||
Location = new Point(12, 296),
|
||
Width = 700,
|
||
ForeColor = Color.DarkSlateGray
|
||
};
|
||
|
||
panel.Controls.Add(button);
|
||
panel.Controls.Add(_input);
|
||
panel.Controls.Add(_rich);
|
||
panel.Controls.Add(_check);
|
||
panel.Controls.Add(radioA);
|
||
panel.Controls.Add(radioB);
|
||
panel.Controls.Add(_combo);
|
||
panel.Controls.Add(listLabel);
|
||
panel.Controls.Add(_list);
|
||
panel.Controls.Add(_tree);
|
||
panel.Controls.Add(_status);
|
||
page.Controls.Add(panel);
|
||
|
||
// ---- page 2: a grid, for the list reader
|
||
var gridPage = new TabPage("表格") { Name = "GridPage" };
|
||
_grid = new DataGridView
|
||
{
|
||
Name = "FixtureDataGrid",
|
||
Dock = DockStyle.Fill,
|
||
AllowUserToAddRows = false,
|
||
ReadOnly = true,
|
||
AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill
|
||
};
|
||
_grid.Columns.Add("序号", "序号");
|
||
_grid.Columns.Add("内容", "内容");
|
||
_grid.Columns.Add("状态", "状态");
|
||
for (int index = 1; index <= 5; index++)
|
||
{
|
||
_grid.Rows.Add(index.ToString(), "第 " + index + " 行内容", index % 2 == 0 ? "已完成" : "待处理");
|
||
}
|
||
|
||
gridPage.Controls.Add(_grid);
|
||
|
||
// ---- page 3: a nested container, for "the list is inside the picked element"
|
||
var nestedPage = new TabPage("嵌套") { Name = "NestedPage" };
|
||
var outer = new Panel { Dock = DockStyle.Fill, Padding = new Padding(8), Name = "NestedOuter" };
|
||
var middle = new Panel { Dock = DockStyle.Fill, Padding = new Padding(8), Name = "NestedMiddle", BackColor = Color.WhiteSmoke };
|
||
var innerList = new ListBox { Dock = DockStyle.Fill, Name = "NestedListBox" };
|
||
for (int index = 1; index <= 4; index++)
|
||
{
|
||
innerList.Items.Add("嵌套列表项 " + index);
|
||
}
|
||
|
||
middle.Controls.Add(innerList);
|
||
outer.Controls.Add(middle);
|
||
nestedPage.Controls.Add(outer);
|
||
|
||
tabs.TabPages.Add(page);
|
||
tabs.TabPages.Add(gridPage);
|
||
tabs.TabPages.Add(nestedPage);
|
||
Controls.Add(tabs);
|
||
|
||
// A second top-level window so "which window" itself can be tested.
|
||
var secondary = new Form
|
||
{
|
||
Text = "UIA 测试夹具 - 第二个窗口",
|
||
Name = "FixtureSecondWindow",
|
||
Owner = this,
|
||
StartPosition = FormStartPosition.Manual,
|
||
Location = new Point(840, 60),
|
||
Size = new Size(360, 220)
|
||
};
|
||
var secondaryButton = new Button
|
||
{
|
||
Text = "第二个窗口的按钮",
|
||
Name = "FixtureSecondButton",
|
||
Location = new Point(20, 24),
|
||
AutoSize = true
|
||
};
|
||
secondaryButton.Click += delegate { SetStatus("第二个窗口的按钮被点击。"); };
|
||
secondary.Controls.Add(secondaryButton);
|
||
secondary.Shown += delegate { secondary.Location = new Point(Location.X + Width + 20, Location.Y); };
|
||
AddOwnedForm(secondary);
|
||
}
|
||
|
||
private void SetStatus(string text)
|
||
{
|
||
_status.Text = DateTime.Now.ToString("HH:mm:ss") + " " + text;
|
||
}
|
||
}
|
||
|
||
internal static class Program
|
||
{
|
||
[STAThread]
|
||
private static void Main()
|
||
{
|
||
Application.EnableVisualStyles();
|
||
Application.SetCompatibleTextRenderingDefault(false);
|
||
Application.Run(new FixtureForm());
|
||
}
|
||
}
|
||
}
|