Question

I am trying to debug some scripts (that use Windows UI Automation support to identify GUI object) that I have developed and that are failing intermittently because they cannot find certain controls in the tree. I'm using also screenshots to check the state of the window that I'm testing and it seems that the controls are there in GUI, but my search inside the tree does not find them (even after a few seconds of sleep). When I use inspect.exe to check the tree, the objects are there.

Is there a way to dump that tree for later analysis? The only way I found until now is by crawling the whole tree recursively, but this is not feasible as it takes an awful lot of time.

Was it helpful?

Solution

Here is my code:

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);
    }
  }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top