-->

AutomationElement /上下文菜单(AutomationElement / Conte

2019-10-19 09:07发布

描述

我想能够使用UI自动化的上下文菜单交互。 基本上,我想:

  • 焦点设置上的AutomationElement
  • SendKeys.SendWait发送SHIFT+F10
  • 看到弹出

我所看到的

我所看到的是, AutomationElement.FindAll(TreeScope.Descendants, Condition.TrueCondition)似乎并没有反映在上下文菜单弹出,即使UISpy看到它。

任何帮助将不胜感激。

下面是我在已经运行的应用实例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);
}

Answer 1:

该上的Shift-F10打开上下文菜单实际上是根元素(桌面)的孩子,所以如果你使用的Descendants(AutomationElement.RootElement).Count的替代Descendants(notepad).Count ,你会看到其中的差别。 例如

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


文章来源: AutomationElement / Context Menus