How to get menu elements from the parent element

Author: Alfonso Alarcón

In Coded UI Test solutions of visual Studio (Enterprise, Ultimate or Premium versions) is recommendable to optimize efforts avoiding recording actions when a large numbers of elements must be recorded. Therefore, the best option is to get the Coded UI controls from the parent control and to get from it all the children items using  Add a new control to the UIMap tool (see https://automatedtestingtools.wordpress.com/2016/04/11/how-to-capture-ui-control-with-coded-ui-test-builder-without-recording-actions).

For example in a typical calculator the menu bar has a set of menus (View, Edit and Help) the strategy to follow is to get only the squared UI Control and from it get both all the menus and related items of each menu.

calculator

In the next class is provided how to click in an element of a menu. Therefore, if you want to click for example in the Programmer item of the menu bar the key point is to use GetChildren():

        public static WinMenuBar MenuBar;
        private static Dictionary<string, WinMenuBar> objDict = UiTestMenuBarDict.UiTestWinMenuBar();
        public static void MouseClickSubMenuItems(string menu, string subMenu, string dictKey)
        {
            MenuBar = objDict[dictKey];

            foreach (WinMenuItem item in MenuBar.Items)
            {
                if (item.FriendlyName == menu)
                {
                    Mouse.Click(item);
                    foreach (WinMenuItem subMenuItem in item.Items)
                    {
                        if (subMenuItem.FriendlyName == subMenu)
                        {
                           Mouse.Click(subMenuItem);
                        }
                    }
                }                
            }
        }

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s