简体   繁体   中英

UI automation and menu item

I am trying to use .NET UI automation. I have a third party application which I know is written in .NET, but I have no source code for. I am starting the application, with Process.Start("exe path"); and getting the processID and then search for the main Application window by

 this.MainWindow = AutomationElement.RootElement.FindFirst
                    (TreeScope.Children,
                     new AndCondition(
                         new PropertyCondition(AutomationElement.ProcessIdProperty, this.ProcessId),
                         new PropertyCondition(AutomationElement.NameProperty, InitialWindowName)
                         ));

this is working find But in the main window, there is a menu bar that has the common "File, Edit, ..."

So, next step I select the menu bar and expand the file menu with

var menuBar = this.MainWindow.FindFirst(TreeScope.Children,
                                      new PropertyCondition(AutomationElement.LocalizedControlTypeProperty, "menu bar"));
                var fileMenu = menuBar.FindAll(TreeScope.Children, Condition.TrueCondition)[0];
                var expandPattern = fileMenu.GetCurrentPattern(ExpandCollapsePattern.Pattern) as ExpandCollapsePattern;
                if (expandPattern.Current.ExpandCollapseState != ExpandCollapseState.Expanded)
                    expandPattern.Expand();
                Thread.Sleep(3000);

Since the "File" menu option is the first option in the menu bar, so this is expanding the "File" menu options

Now, I want to call the print menu item in the "File" menu list.

the print menu item has the name "Print Document Ctrl+P"

so I search for

var printMenuItem = this.MainWindow.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.NameProperty,"Print Document   Ctrl+P"));

But without success. I tried different way, like getting all items descendants and looping through the names to find if they have "Print" in them without success, like this

var list = this.MainWindow.FindAll(TreeScope.Descendants, new PropertyCondition(AutomationElement.LocalizedControlTypeProperty,"menu item"));
for (int i = 0; i < list.count; i++)
{
   if (list[0].Current.Name.IndexOf("Print") > -1)...

我尝试了这个,并能够找到打印菜单

var printMenu = fileMenu.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.NameProperty, "Print"));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM