Forum Discussion

obaid_shirwani's avatar
obaid_shirwani
Contributor
9 years ago
Solved

How to define global variables using scripts

Hi folks,

 

I want to declare and use self defined global variables across a complete test flow execution. The Test Flows encompass multiple script (JScript) files and multiple functions (I dont want to use Project or Project Suite Variables).

 

I tried to do the following but its not working:

 

//Defined a script file variables.sj. This file had this code:

 

var myGlobalVariable = 9;
Log.Message("01. Global value of 'myGlobalVariable': " + myGlobalVariable);

function setVariables()
{
myGlobalVariable = 10;
Log.Message("02. Local value of 'myGlobalVariable': " + myGlobalVariable);
}

Log.Message("03. Global value of 'myGlobalVariable': " + loginArray);

 

//Defined another script file useGlobalVariables.sj. This file had this code:

 

 

//USEUNIT variables

function useGlobalVariablesFunction()
{
variables.setVariables();

//I know this function above will not set the values outside its scope.

 

log.Message("Calling global variable: " + variables.myGlobalVariable);
}

 

 

 

Executing the function 'useGlobalVariablesFunction' throws an error: 'Object doesn't support this property or method'

 

Regards,

 

OS

  • obaid_shirwani's avatar
    obaid_shirwani
    9 years ago

    Issue resolved. It was a typo error :( my bad

4 Replies

  • HKosova's avatar
    HKosova
    SmartBear Alumni (Retired)

    Assuming

     

    Log.Message("03. Global value of 'myGlobalVariable': " + loginArray);
    // should be myGlobalVariable

    is just a typo, your code works fine:

     

     

    log.png

     

    Nothing in this example would cause the error you're describing. Is this the actual code you're using? Do you use unique names for script units, functions and variables?

    • obaid_shirwani's avatar
      obaid_shirwani
      Contributor

      Thank you for the response Helen.

       

      Yes, it was a type.

      Yes, I use unique names all out (Functions signatures / file names / variables etc)

       

      Executing the function: 'useGlobalVariablesFunction' is throwing an error for me.

      • obaid_shirwani's avatar
        obaid_shirwani
        Contributor

        Issue resolved. It was a typo error :( my bad