我正在尝试调试一些脚本(使用Windows UI自动化支持来识别GUI对象),我已经开发并且间歇性失败,因为它们无法在树中找到某些控件。我还使用屏幕截图来检查我正在测试的窗口状态,似乎控件在GUI中,但我在树内的搜索没有找到它们(即使经过几秒钟的睡眠)。当我使用inspect.exe检查树时,对象就在那里。
是否有办法转储该树以供以后分析?到目前为止,我找到的唯一方法是递归地爬行整个树,但这是不可行的,因为它需要大量的时间。
下面是我的代码:
public static string DumpUIATree(this AutomationElement element, bool dumpFullInfo = false)
{
var s = element.Name() + " : " + element.ControlType().ProgrammaticName;
DumpChildrenRecursively(element, 1, ref s, dumpFullInfo);
return s;
}
private static List<AutomationElement> GetChildNodes(this AutomationElement automationElement)
{
var children = new List<AutomationElement>();
TreeWalker walker = TreeWalker.ControlViewWalker;
AutomationElement child = walker.GetFirstChild(automationElement);
while (child != null)
{
children.Add(child);
child = walker.GetNextSibling(child);
}
return children;
}
private static void DumpChildrenRecursively(AutomationElement node, int level, ref string s, bool dumpFullInfo = false)
{
var children = node.GetChildNodes();
foreach (var child in children)
{
if (child != null)
{
for (int i = 0; i < level; i++)
s += "-";
s += " " + child.Name() + " : " + child.ControlType().ProgrammaticName + "rn";
if (dumpFullInfo)
{
foreach (var prop in child.GetSupportedProperties())
{
s += " > " + prop.ProgrammaticName + " = " + child.GetCurrentPropertyValue(prop) + "rn";
}
s += "rn";
}
DumpChildrenRecursively(child, level + 1, ref s, dumpFullInfo);
}
}
}