CodedUI、SpecFlow:尝试从按钮获取菜单选项

CodedUI, SpecFlow: Trying to get the menu options from a button

我的应用程序中有一个带下拉菜单的按钮和 2 个 options/menuitems。我可以使用 codedUI 单击按钮,但无法单击按钮中的菜单项。按照我为此目的编写的 C# 代码。

    WinButton _messageMenuItem = new WinButton(ToolBar);
   _messageMenuItem.SearchProperties.Add(WinButton.PropertyNames.Name, "Messages (4)");
   _messageMenuItem.WindowTitles.Any(s => regex.IsMatch(s));
   Click(_messageMenuItem);//**Works**
   WinWindow _messagesdropdown = new WinWindow(MessageMenuItem);
   _messagesdropdown.SearchProperties.Add(WinWindow.PropertyNames.Name, "Messages (4)DropDown"); //DropDown
   _messagesdropdown.WindowTitles.Any(s => regex.IsMatch(s));
   Click(_messagesdropdown);//**Doesnt work**
   UITestControlCollection AllMenuItems = _messageMenuItem.GetChildren();
   UITestControlCollection AllFirstMenuItems = _messagesdropdown.GetChildren();
   //Click(AllMenuItems[0]);//**Doesnt work**
   //Click(AllFirstMenuItems[0]);//**Doesnt work**
   WinMenuItem _textMessagesMenuItem = new WinMenuItem();
   _textMessagesMenuItem.SearchProperties.Add(WinMenuItem.PropertyNames.Name, "Text Messages (4)"); //Text
   _textMessagesMenuItem.WindowTitles.Any(s => regex.IsMatch(s));
   return _textMessagesMenuItem;

我通过在 Coded UI 中记录控件并使用为 WinMenu 项生成的 class 来修复它。所以点击按钮之前的代码保持不变。

WinButton _messageMenuItem = new WinButton(ToolBar);
_messageMenuItem.SearchProperties.Add(WinButton.PropertyNames.Name, "Messages (4)");
_messageMenuItem.WindowTitles.Any(s => regex.IsMatch(s));
Click(_messageMenuItem);//**Works**

但在这之后,UI测试的记录被用来获取菜单项

public class UIItemWindow : WinWindow
        {
            //Use this button for UI Messages window
            public UIItemWindow()
            {
                #region Search Criteria
                this.SearchProperties.Add(WinWindow.PropertyNames.AccessibleName,"Messages (4)DropDown");
                this.SearchProperties.Add(new PropertyExpression(WinWindow.PropertyNames.ClassName, "WindowsForms10.Window", PropertyExpressionOperator.Contains));
                #endregion
            }

            #region Properties
            public WinMenuItem UITextMessages4MenuItem
            {
                get
                {
                    if ((this.mUITextMessages4MenuItem == null))
                    {
                        this.mUITextMessages4MenuItem = new WinMenuItem(this);
                        #region Search Criteria
                        this.mUITextMessages4MenuItem.SearchProperties.Add(WinMenuItem.PropertyNames.Name,"Text Messages (4)");
                        #endregion
                    }
                    return this.mUITextMessages4MenuItem;
                }
            }

点击动作执行如下

UIItemWindow testWindow = new UIItemWindow();
WinMenuItem _textMessagesMenuItem = testWindow.UITextMessages4MenuItem;
Mouse.click(_textMessagesMenuItem );//**Works**