Forum Discussion

oychw's avatar
oychw
Contributor
14 years ago

Could we click Menu use actual name?

Could we click Menu use actual name?



    I recording a "click on Menu"( TMenuItem,delphi),like following:

        Aliases.UserCard.frm_UserCardNewMain.MainMenu.Click("File|Add");

    "File" and "Add" is caption of the item. could we use item's name to replace them?

thanks!

4 Replies

  • Hello,
     





      1. If you need just to invoke actions assigned to menu items, you can access the corresponding items using the VCLObject native method and call their native Click method.






    Please note that TestComplete calls native methods of tested application objects synchronously. It means that if an object's native method shows a modal dialog, TestComplete won't be able to continue executing the test until the dialog is closed. A solution is to call the object's native methods asynchronously using the Runner.SetObjectPropertyAsync method:









       Runner.CallObjectMethodAsync( mainForm.VCLObject("File_Open"), "Click");









    Please find more information on this in the Calling Methods Asynchronously help topic.
     





      2. Could you please tell me the reason why you need to work with menu items by their names rather than captions? I assume that you are testing the application with multilingual support, so the captions are different for different localizations, right? If so, probably, I could suggest a better solution for you.
  • Alex:

        thank you very much! We are testing the application with multilingual support.so the captions are different for different localizations.

        I am glad to hear that you can  suggest a better solution.would you please tell what it is? thanks!



    Best regards,

    Rongzhong

  • Hello,





      As you know, the MainMenu.Click method requires the path to a menu item as a parameter. The path can be built based either on menu item captions, or on menu item indices. It's possible to obtain necessary indices/captions for a menu item by using the native TMenuItem.Parent method. 

    The following helper routine illustrates how to form a valid path for a menu item:







    function GetPathByMenuItem(item){ 

      var index =  "[" + item.MenuIndex + "]";  

      var path = index;

      while( item.NativeDelphiObject != null && item.NativeDelphiObject.Parent != null){

        item = item.NativeDelphiObject.Parent;

        path = "[" + item.MenuIndex + "]|" + path;

      }

      return path;

    }





    //usage

      var mainMenu =  Sys.Process("Orders").VCLObject("MainForm").MainMenu;

      var item =    Sys.Process("Orders").VCLObject("MainForm").VCLObject("File_New")

      mainMenu.Click(GetPathByMenuItem(item));







    So, your script will be language-independent, but will use TestComplete's MainMenu.Click method to perform clicks visually.

    Does this work for you?