Search code examples
c#coded-ui-testsspecflow

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. I am able to click the button using codedUI but I am unable to click the menuitems from the button. Following the C# code that I wrote for the purpose.

enter image description here

    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;

Solution

  • I fixed it by recording the control in Coded UI & using the generated class for the WinMenu Item. 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

    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**