UI automation and menu item

.net, c#, ui-automation

Solution

I tried this and was able to find the print menu

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

Problem

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)... ```

Original source