Forum Discussion

pashooo's avatar
pashooo
Contributor
14 years ago

definition variables when them are not exist yet

In my TC project I have created file "definition".



it contain such definitions of variables:

main_menu = Sys.Process("OptionsWorkshop").WPFObject("HwndSource: MainForm").WPFObject("MainForm").WPFObject("DockPanel", "", 1).WPFObject("ToolBarTray").WPFObject("FeaturesToolBar");

... and others.



So, when I run the first file of my test:

function PLZ_cnct()

{


TestedApps.OptionsWorkshop.Run();

or

TestedApps.OptionsWorkshop_Launcher.Run(1, true);  


...some code...

}

... I have got an error in the line:

 main_menu = Sys.Process("OptionsWorkshop").WPFObject("HwndSource: MainForm").WPFObject("MainForm").WPFObject("DockPanel", "", 1).WPFObject("ToolBarTray").WPFObject("FeaturesToolBar");

... in definition file.



The error means that test try to find main_menu object before starting  the app.

How can I  explain to TC that to find defined variables after starting?





  • tristaanogre's avatar
    tristaanogre
    Esteemed Contributor
    You cannot assign objects to a variable if the object does not exist in memory.  So, you'll need to make sure that these assignments are wrapped in a script routine like



    function AssignVariables()

    {

        main_menu = ...

        main_form = ...



    }




    However, this may still cause problems if the objects are not created all up front.  For example, say you have a form defined in AssignVariables the does not exist until you click a button.  You'll still end up with your error in that case.



    It sounds like you're trying to build an object repository on the fly.  It is possible to do so but you need to do a lot more than just assigning variables.  You need to make sure you relate things to other things and make sure that you refresh the variables on occasion in case the handles and such have shifted.



    A better, solution, is to use the NameMapping feature of TestComplete as this will allow you to pre-defined all your objects and map them to Aliases ahead of time.  Then you can just simply call things like



    Aliases.main_menu

    Aliases.main_form




    You can check out this video:



    http://smartbear.com/support/screencasts/testcomplete/reliable-tests-for-dynamic-objects/



    I'd also recommend reading the articles starting with



    http://smartbear.com/support/viewarticle/11508/http://smartbear.com/support/viewarticle/11508/
  • Hi Pasha,



    How can I explain to TC that to find defined variables after starting?


    Any script code in the global scope - that is, outside of functions - is executed before TestComplete starts running a function. It looks like this:

    var n = 42;



    function Test()

    {

      Log.Message(n); // 42

    }


    However, when using the Sys.Process(...).WPFObject(...) notation, you can't refer to objects that don't actually exist, that is why, the error occurs.



    A possible approach to accomplish your task is to create and use Name Mapping ("object repository") instead as Robert suggested.



    If you'd prefer not to use Name Mapping, you can store object names as strings and then convert these strings to the corresponding objects using eval:

    strMainMenu = 'Sys.Process("OptionsWorkshop").WPFObject("HwndSource: MainForm").WPFObject("MainForm").WPFObject("DockPanel", "", 1).WPFObject("ToolBarTray").WPFObject("FeaturesToolBar")';



    function Test()

    {

      ...



      main_menu = eval(strMainMenu);

      // Then, do something with main_menu

      ...

    }