简体   繁体   English

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

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

There is a button with a dropdown and 2 options/menuitems in my application.我的应用程序中有一个带有下拉菜单和 2 个选项/菜单项的按钮。 I am able to click the button using codedUI but I am unable to click the menuitems from the button.我可以使用 codedUI 单击按钮,但无法单击按钮中的菜单项。 Following the C# code that I wrote for the purpose.遵循我为此目的编写的 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;

I fixed it by recording the control in Coded UI & using the generated class for the WinMenu Item.我通过在编码 UI 中记录控件并使用 WinMenu 项的生成类来修复它。 So the code upto clicking the button remains the same.所以点击按钮之前的代码保持不变。

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

But after this, recording from UI test is used to get the menu item但在此之后,从 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;
                }
            }

Click action is performed as follows点击动作执行如下

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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