TL; DR:我做错了什么导致工作区窗格显示在Inspect Objects中但未显示在我的自定义代码中?
我正在尝试为第三方程序编写一些UI自动化.我正在使用Windows SDK附带的Inspect.exe,我已经尝试过System.Windows.Automation和直接COM调用(使用UIA Verify的包装器库).
Process[] processes = Process.GetProcessesByName("Redacted Client");
if (processes.Length == 0) throw new Exception("Could not find \"Redacted Client\" process");
PropertyCondition parentFileCond = new PropertyCondition(AutomationElement.ProcessIdProperty, processes[0].Id);
PropertyCondition workspaceCond = new PropertyCondition(AutomationElement.NameProperty, "Workspace", PropertyConditionFlags.IgnoreCase);
PropertyCondition documentCond = new PropertyCondition(AutomationElement.NameProperty, "Untitled3", PropertyConditionFlags.IgnoreCase);
var parentElement = AutomationElement.RootElement.FindFirst(TreeScope.Children, parentFileCond);
var workspaceElement = parentElement.FindFirst(TreeScope.Children, workspaceCond); //Also does not work with TreeScope.Descendants
var documentElement = workspaceElement.FindFirst(TreeScope.Children, documentCond);
Run Code Online (Sandbox Code Playgroud)
当我尝试上面的代码时,parentElement确实有对主程序窗口的正确引用,但是workspaceElement为null. …
我正在尝试在我没有源的遗留应用程序上自动化一些东西。所以我基本上是在尝试使用 Windows API 来单击我需要的按钮。
有一个msvb_lib_toolbar看起来像这样的工具栏:
我可以通过使用以下代码来处理它(我认为):
IntPtr window = FindWindow("ThunderRT6FormDC", "redacted");
IntPtr bar = FindWindowEx(window, IntPtr.Zero,"msvb_lib_toolbar",null);
Run Code Online (Sandbox Code Playgroud)
查看文档,似乎我应该能够使用SendMessage和TB_PRESSBUTTON单击这些按钮的消息:
[DllImport("user32.dll")]
public static extern int SendMessage(int hWnd, uint Msg, int wParam, int lParam);
Run Code Online (Sandbox Code Playgroud)
但是,我不确定如何设置wParam并lParam单击栏上的所需按钮。该文档似乎也没有多大帮助。
您能否提一些建议?
根据评论,我也尝试过UIAutomation. 我可以使用以下代码找到工具栏:
AutomationElement mainWindow = AutomationElement.RootElement.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.NameProperty, "Migration Expert"));
AutomationElement toolbar = mainWindow.FindFirst(TreeScope.Subtree, new PropertyCondition(AutomationElement.ClassNameProperty, "msvb_lib_toolbar"));
Run Code Online (Sandbox Code Playgroud)
但是从这里开始,我不确定该怎么做,因为 Spy++ 没有显示此对象的其他子项:
看着这个Current属性,AutomationElement我看不到任何东西跳出来,但BoundingRectangle似乎表明我找到了正确的元素。
使用inspector.exe也不会指示工具栏上的任何子项。
我正在尝试使用 MS UI Automation 来测试 WPF 应用程序,并使用 Windows SDK 中包含的检查对象工具 (inspect.exe) 来查找某些元素的 AutomationId 属性。
Inspect 对我来说表现得很奇怪:
如果我关闭所有应用程序并启动 WPF 应用程序和检查,检查将能够查看各种 UI 元素的 AutomationId 属性。没有 AutomationId 的元素仅显示两个引号,表示空字符串 ("")。
在 WPF 应用程序中执行一些操作后,inspect.exe 挂起,我必须终止它并重新启动它。尽管机器的 CPU 和 RAM 利用率约为 50% 或更低,但我尝试等待几分钟(有几次可能接近 20 或 30 分钟),但无济于事。
我尝试按照/sf/answers/548360991/中的建议正常运行检查并以管理员身份运行检查,并且无论哪种方式,它的行为都是相同的。
如果有的话,我做错了什么?我是否太不耐烦了,我是否需要等待很长时间而不是假设检查已挂起?为什么检查关于 AutomationId 的行为会有所不同?