我正在尝试调试我开发的一些脚本(使用Windows UI自动化支持来识别GUI对象),这些脚本间歇性地失败,因为它们无法在树中找到某些控件。我还使用屏幕截图来检查我正在测试的窗口的状态,似乎GUI中有控件,但我在树内的搜索找不到它们(即使在几秒钟的睡眠后)。当我使用inspect.exe检查树时,对象就在那里。
有没有办法转储该树以供日后分析?我到目前为止找到的唯一方法是递归地爬行整棵树,但这是不可行的,因为它需要花费大量的时间。
答案 0 :(得分:2)
这是我的代码:
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 + "\r\n";
if (dumpFullInfo)
{
foreach (var prop in child.GetSupportedProperties())
{
s += " > " + prop.ProgrammaticName + " = " + child.GetCurrentPropertyValue(prop) + "\r\n";
}
s += "\r\n";
}
DumpChildrenRecursively(child, level + 1, ref s, dumpFullInfo);
}
}
}