Forum Discussion

ilija_panov's avatar
ilija_panov
Contributor
13 years ago

using global variables to pass data between tests

Hi to All,



I have a variable accounts of custom dictionary type in JScript which is filled by an ADO function from a database. So there is no predefined project variable type ( string, table, DBTable, Object) which can server to store this variable and pass it between test script units.



Also have a requirment for recursovely traversing the test scripts with a DDT excel driver so it is important to fill the accounts global variable to be used in thje rest fo the tests in every DDT cycle.



The problem is that for a variable to be global in TC it is neccesary to be defined out of a function. In this case I have no fill this variable  since if it's not in a function it cannot be called from Test Items. Would really appreciate some help with global variables 



Thanks,

  Ilija


  • AlexKaras's avatar
    AlexKaras
    Icon for Champion Level 3 rankChampion Level 3
    Hi Ilija,



    Use the OnStartTest event (http://support.smartbear.com/viewarticle/11118/ and http://support.smartbear.com/viewarticle/15535/) to populate the variable with the data. The event is triggered when TestComplete starts a test run and with a simple check you can test if the variable was already initialized or not. The variable should be either ProjectSuite or Project of object type. Variable of object type does not mean that it stores a reference to some object of the tested application, but means that the variable can store the reference to any programmatic object or entity (http://support.smartbear.com/viewarticle/18921/ and http://support.smartbear.com/viewarticle/11858/).
  • Thanks Alex,



    I'm making some progress with the OnStartTest event, it seems to be evoked in the recursive funciton, but i still don't know whether to use   = or some other function with the object variable type for assigning it to local variables in tests.



    For  example, the event looks like



    //USEUNIT database



    function GeneralEvents_OnStartTest(Sender)

    {

      var accounts = database.getAccounts(Project.Variables.user);

      Project.Variables.accounts = accounts;

      if (Project.Variables.accounts != null)

      {



         Log.Message('accouts filled');

       

      }

    }



    and the simple checks confirms that the Project.Variables.accounts is not null



    but whenm i try to use the accounts object type global variables in tests



    ....

      var accounts = Project.Variables.accounts; 

    var i;

    for (var num_cue_s in logon.accounts)  

      {

      panel2.panel.form.panelTablerowBackgroundlightgray.panelRightcolumn.selectAccountsdropdownlist.ClickItem(accounts[num_cue_s]);

          i++;

          if (i > 1) break;

      } 



    it seems to not work, athoulgh I think i'm really close to succesfully using the object variables for passing it between tests



    Thanks in advance,

       Ilija
  • Forgot to mention that



        var accounts = Project.Variables.accounts;

        Log.Message(accounts);



    doesnt fail, but simply states  [object Object] in the test log.



    I can assume it keeps the objects in some propriatery object class and
    not the one form the source object (in my case is JScript dictionary
    with string key and value pairs), so probably it will be neccesary to
    write a script to convert the objects to\from the propriatery class
    unless there are some function available istead of assigning it with =
  • AlexKaras's avatar
    AlexKaras
    Icon for Champion Level 3 rankChampion Level 3
    Hi Ilija,



    > [...] JScript dictionary
    with string key and value pairs

    I'm not an expert with JScript, but there is a note in the "Supported Scripting Languages - Specifics of Usage" help topic as for JScript arrays. I would recommend you to have a look at this topic if you haven't done this yet.

    Just in case, a quote from there that might be relevant:

    ============

    The format of arrays created by the JScript engine differs from arrays created in VBScript or DelphiScript. The arrays in JScript, C++Script and C#Script are objects, while arrays created in VBScript and DelphiScript are Variant values. This difference limits the use of some routines (for instance, the AddNamedChild method) in JScript, C++Script and C#Script code. In order to use a JScript array with these routines, you need to convert the array to the format adopted in VBScript and DelphiScript. Below is a typical conversion routine: [...]

    ============