AutomationElement / Context Menu

Description

I am trying to interact with context menus using UI Automation. Basically, I try:

  • set focus to AutomationElement
  • SendKeys.SendWait to send SHIFT+F10
  • see what pops up

What I see

What I see is something that AutomationElement.FindAll(TreeScope.Descendants, Condition.TrueCondition)does not seem to be reflected when the context menu is displayed, even if UISpy sees it.

Any help would be greatly appreciated.

Example

Here is an example application that I used in LINQPad :

void Main()
{
  var notepad = FindNotepad();
  Console.WriteLine("Pre-Context: {0}", Descendants(notepad).Count);
  TypeInto(notepad, "(+{F10})");
  Thread.Sleep(1000);

  Console.WriteLine("With Context: {0}", Descendants(notepad).Count);
  TypeInto(notepad, "{ESC}");
  Console.WriteLine("Post-Context: {0}", Descendants(notepad).Count);
}

AutomationElement FindNotepad(string title = "Untitled - Notepad")
{
  var notepadName = new PropertyCondition(AutomationElement.NameProperty, title);
  return AutomationElement.RootElement.FindFirst(TreeScope.Children, notepadName);
}

void TypeInto(AutomationElement element, string keys)
{
  element.SetFocus();
  SendKeys.SendWait(keys);
}

AutomationElementCollection Descendants(AutomationElement element)
{
  return element.FindAll(TreeScope.Subtree, Condition.TrueCondition);
}
+1
source share
1 answer

, Shift-F10, ( ), , Descendants(AutomationElement.RootElement).Count Descendants(notepad).Count, . .

Pre-Context: 2019
With Context: 2036
Post-Context: 2019
0

All Articles