我为 wpf 应用程序自动化 ui 测试。在做了一些步骤之后,我试图找到控件,但是当我使用 FindFirst 或 FindAll 方法时,窗口中的许多控件都丢失了。现在,我做了一个实验: 第一部分:
System.Diagnostics.Process.Start(@"C:\application.exe");
Thread.Sleep(2000);
Condition windowCondition = new PropertyCondition(AutomationElement.NameProperty, "Window Title");
AutomationElement window = AutomationElement.RootElement.FindChildByCondition(windowCondition);
window.SetFocus();
int x = 512;
int y = 384;
SetCursorPos(x, y);
// Click on coordinates:
mouse_event(MOUSEEVENTF_LEFTDOWN, x, y, 0, 0);
mouse_event(MOUSEEVENTF_LEFTUP, x, y, 0, 0);
之后,我可以找到我需要的所有控件,并且所有工具(UISpy、UIAVerify ant others)都可以找到所有控件。
第 2 部分:我只添加了 2 个字符串:
System.Diagnostics.Process.Start(@"C:\application.exe");
Thread.Sleep(2000);
Condition windowCondition = new PropertyCondition(AutomationElement.NameProperty, "Window Title");
AutomationElement window = AutomationElement.RootElement.FindChildByCondition(windowCondition);
// New strings here:
Condition controlCondition = new PropertyCondition(AutomationElement.AutomationIdProperty, "ConnectButton");
AutomationElement control = window.FindFirst(TreeScope.Descendants, controlCondition);
window.SetFocus();
int x = 512;
int y = 384;
SetCursorPos(x, y);
// Click on coordinates:
mouse_event(MOUSEEVENTF_LEFTDOWN, x, y, 0, 0);
mouse_event(MOUSEEVENTF_LEFTUP, x, y, 0, 0);
现在,我可以发现那些控件并不真正可见,而那些可见的 - 未检测到。
怎么了?谢谢