Need help with submenus

Ask general questions here.
chrisgeorge
Posts: 49
Joined: Thu Aug 20, 2009 11:28 am

Need help with submenus

Post by chrisgeorge » Tue Nov 03, 2009 12:00 pm

Hi,

We've having real problems operating on submenus of context menus. We have items in a Repository for both the parent and the child menu, both of which have basepaths of /contextmenu.

However, we can click on menu items on the parent, but it just can't find anything on the sub menu.

Is there a recommended way of reliably dealing with submenus? Can it all be done programmatically by getting "child" menus of a specific menu, then iterating through menuitems?

Help!! cause this has already taken cumulatively over a day of time trying to fix this.

Cheers

Chris
Chris George
Test Engineer

Red Gate Software Ltd
Cambridge

User avatar
Ciege
Posts: 1336
Joined: Thu Oct 16, 2008 6:46 pm
Location: Arizona, USA

Re: Need help with submenus

Post by Ciege » Tue Nov 03, 2009 4:09 pm

Here is the framework code I have written for menu clicks and menu verifications. These are working against my .NET AUT reliably. If your menubar control is named differently you may need to modify the finds in the methods themselves.

To call (for example) the menuclick method do somthing like the following:

Code: Select all

string[] strMenuToClick;
strMenuToClick = new string[2];
strMenuToClick[0] = "File";
strMenuToClick[1] = "Print";
intResult = RFW.MenuClick(HDClientForm, strMenuToClick);

Code: Select all

        public static int MenuClick(Ranorex.Form RanorexFormName, string[] strMenuItemsToClick)
        {
            /************************************************************************
             * Function         : MenuClick(Ranorex.Form RanorexFormName, string[] strMenuItemsToClick)
             *
             * Description      : This function will select a menu item or menu item -> submenu item  
             *                  :  that lives on the form.
             *                         
             * Parameters       : RanorexFormName       - Ranorex.Form Object of window that contains the menu
             *                  : strMenuItemsToClick   - string array tof the menu item and sub menu item to click
             *                  :                       - EXAMPLE: 
             *                  :                       - To click the File menu set strMenuItemsToClick[0] = "File"
             *                  :                       - To click the File->Open menu set strMenuItemsToClick[0] = "File" and strMenuItemsToClick[1] = "Open"
             * 
             * Return Type      : Integer
             * 
             * Return Value     : 0 for success, -1 for failure
             * 
             * Revision History
             * Date       : Author                    : Reason/Change
             * ---------- : ------------------------- : ------------------------------
             * 07/01/2009 : Chris Gendreau            : Initial Creation 
            ************************************************************************/
            Ranorex.MenuBar HDMenuBar = null;

            try
            {
                //Search for the menu bar on the Form
                try
                {
                    RanorexFormName.EnsureVisible();
                    RanorexFormName.Activate();
                    Thread.Sleep(500);
                    HDMenuBar = RanorexFormName.FindSingle(".//menubar[@controlname='mMenu']", 30000);
                }

                catch (RanorexException e)
                {
                    Report.Error("Unable to find Menu Bar on the form: " + RanorexFormName);
                    Report.Error(e.ToString());
                    Report.Screenshot();
                    return -1;
                }

                //Click the menu item(s)
                foreach (string MenuItem in strMenuItemsToClick)
                {
                    try
                    {
                        Report.Debug("Clicking Menu Item: " + MenuItem);
                        HDMenuBar[MenuItem].Click(Location.Center);
                        Report.Debug("  Clicked Menu Item: " + MenuItem);
                        Thread.Sleep(500);
                    }

                    catch (RanorexException e)
                    {
                        Report.Error(e.ToString());
                        Report.Screenshot();
                        return -1;
                    }
                }
                return 0;
            }
            catch (Exception e)
            {
                Report.Error(e.ToString());
                return -1;
            }
        }//End MenuClick

Code: Select all

        public static int VerifyMenuItem(Ranorex.Form RanorexFormName, string[] strMenuToVerify)
        {
            /************************************************************************
             * Function         : VerifyMenuItem(Ranorex.Form RanorexFormName, string[] strMenuToVerify)
             *
             * Description      : This function will verify a menu item or menu item -> submenu item  
             *                  :  exists on the form.
             *                         
             * Parameters       : RanorexFormName       - Ranorex.Form Object of window that contains the menu
             *                  : strMenuToVerify       - string array tof the menu item and sub menu item to verify
             *                  :                       - EXAMPLE: 
             *                  :                       - To verify the File menu set strMenuToVerify[0] = "File"
             *                  :                       - To verify the File->Open menu set strMenuToVerify[0] = "File" and strMenuToVerify[1] = "Open"
             * 
             * Return Type      : Integer
             * 
             * Return Value     : 0 for success, -1 for failure
             * 
             * Revision History
             * Date       : Author                    : Reason/Change
             * ---------- : ------------------------- : ------------------------------
             * 08/31/2009 : Chris Gendreau            : Initial Creation 
            ************************************************************************/
            Ranorex.MenuBar HDMenuBar = null;

            try
            {
                //Search for the menu bar on the Form
                try
                {
                    RanorexFormName.EnsureVisible();
                    RanorexFormName.Activate();
                    Thread.Sleep(500);
                    HDMenuBar = RanorexFormName.FindSingle(".//menubar[@controlname='mMenu']", 30000);
                }

                catch (RanorexException e)
                {
                    Report.Error("Unable to find Menu Bar on the form: " + RanorexFormName);
                    Report.Error(e.ToString());
                    Report.Screenshot();
                    return -1;
                }

                //Verify the menu item(s)
                string strMenusText = null;
                string strMenusXPath = null;
                int intLength = strMenuToVerify.Length;

                //Create string of menu items and XPath of menu items
                foreach (string MenuItem in strMenuToVerify)
                {
                    strMenusText = strMenusText + MenuItem;
                    strMenusXPath = strMenusXPath + "/menuitem[@accessiblename='" + MenuItem + "']";

                    if (MenuItem != strMenuToVerify[intLength - 1])
                    {
                        strMenusText = strMenusText + " -> ";
                    }
                }
                Report.Debug("Verifying Menu Item: " + strMenusText + " exists.");

                try
                {
                    Ranorex.MenuItem HDMenuItem = HDMenuBar.FindSingle(".//" + strMenusXPath, 10000);
                }

                catch (RanorexException e)
                {
                    Report.Debug("Menu Item : " + strMenusText + " does not exist");
                    return -1;
                }

                Report.Debug("Verified Menu Item : " + strMenusText + " exists");
                return 0;
            }
            catch (Exception e)
            {
                Report.Error(e.ToString());
                return -1;
            }
        }//End VerifyMenuItem
If this or any response has helped you, please reply to the thread stating that it worked so other people with a similar issue will know how you fixed your issue!

Ciege...

User avatar
Support Team
Site Admin
Site Admin
Posts: 12145
Joined: Fri Jul 07, 2006 4:30 pm
Location: Houston, Texas, USA
Contact:

Re: Need help with submenus

Post by Support Team » Tue Nov 03, 2009 7:36 pm

Did you try using the Ranorex Recorder to click on menu and submenu items?
Does the recorder replay work?
How do the paths for the individual menu/submenu items look like, could you give some examples?
Did you try spying the menu/submenu items using Ranorex Spy?

A Ranorex snapshot of the context menu and the submenus could also help. Could you please create snapshots for the menus and submenus and post them in the forums or send it to support_at_ranorex.com? Please, read the following section in the Ranorex User Guide on how to create snapshots of popup/context menus: http://www.ranorex.com/support/user-gui ... html#c2072

Regards,
Alex
Ranorex Support Team

chrisgeorge
Posts: 49
Joined: Thu Aug 20, 2009 11:28 am

Re: Need help with submenus

Post by chrisgeorge » Fri Nov 06, 2009 9:30 am

Ciege...

OMG! It works! How?? Click on one menu item brings up another contextmenu object which does not seem to be related to the first one and differs by instance number.... yet your simple method just works!!!

I'm confused (slightly annoyed that I didn't find this solution earlier ;-), yet deliriously happy that it just works!

Thankyou.

Would anyone care to explain how the hell this actually works!!


Cheers

Chris
Chris George
Test Engineer

Red Gate Software Ltd
Cambridge

User avatar
Support Team
Site Admin
Site Admin
Posts: 12145
Joined: Fri Jul 07, 2006 4:30 pm
Location: Houston, Texas, USA
Contact:

Re: Need help with submenus

Post by Support Team » Fri Nov 06, 2009 12:11 pm

Ciege just uses the Ranorex API, that's all :-)

Sometimes, the Ranorex tools don't create RanoreXPaths that will work for every situation an element can be in. E.g. for your context menu, the instance numbers are different when the sub menu pops up on the right or on the left of the parent menu. However, nor the Recorder/Spy can handle all those situations, simply because they don't know all the situations an element can be in, it just knows how to identify the element right at the moment of recording/tracking.

That's why sometimes it might be better to use the Ranorex API directly. For menubars, for example, you could just get a menu item inside the menubar by using the MenuItem indexer, i.e. MenuItem["myMenuItemName"] like Ciege does in his code. The same works for (context) menus. The code for clicking on a menu item inside a menu bar, then clicking inside the popped up contextmenu and then again into another popped up contextmenu using the API could look like the following:
MenuBar menuBar = ...; // your code to retrieve the menu bar, e.g. repository
// click on "File" item in menubar
menuBar["File"].Click();
 // click on "New" item in context menu
ContextMenu.Current["New"].Click();
 // click on "File..." item in child context menu
ContextMenu.Current["File..."].Click();
You see that the hand-written code might be much simpler than the Recorder-generated one. And it can be more reliable as well.

Regards,
Alex
Ranorex Support Team

User avatar
Ciege
Posts: 1336
Joined: Thu Oct 16, 2008 6:46 pm
Location: Arizona, USA

Re: Need help with submenus

Post by Ciege » Fri Nov 06, 2009 3:58 pm

chrisgeorge wrote:Ciege...

OMG! It works! How?? Click on one menu item brings up another contextmenu object which does not seem to be related to the first one and differs by instance number.... yet your simple method just works!!!

I'm confused (slightly annoyed that I didn't find this solution earlier ;-), yet deliriously happy that it just works!

Thankyou.

Would anyone care to explain how the hell this actually works!!


Cheers

Chris
Hi Chris,

Since Alex did such a good job of describing how this works I won't go any further into it. However, I do want to say to you not to get annoyed for not having done this code yourself yet... I only got to it after a bit of trial and error with Ranorex. I've been using Ranorex for less than a year but considering Ranorex is such an open and powerful tool I know there is almost always a way to do what you want to do. It just takes a little time and patience to learn the intricacies of Ranorex, but once you start to understand how it works your whole world of automation just opens up. It's a great tool so don't give up!

Also remember, the dev and support team at Ranorex are second to none... They are always right on top of giving answers, pointers and suggestions whenever you need them so don't hesitate to ask them here in the forums. If it weren't for their help I wouldn't be where I am at now. Oh yea, I'm here a lot to so if I see something that I can lend a hand with while the guys across the pond are sleeping I'll do my best...

Oh ya, thank you for the kind words... And you are welcome for the help!

Take care...
If this or any response has helped you, please reply to the thread stating that it worked so other people with a similar issue will know how you fixed your issue!

Ciege...

chrisgeorge
Posts: 49
Joined: Thu Aug 20, 2009 11:28 am

Re: Need help with submenus

Post by chrisgeorge » Mon Nov 09, 2009 9:11 am

Thanks Guys. Although I got annoyed I won't give up on it! Ranorex is the best gui automation framework I've used in a very long time so I'm not prepared to jack it in just yet ;-)

My menu operations are working luuuurrvely now!!

Chris
Chris George
Test Engineer

Red Gate Software Ltd
Cambridge