Ask a Question

How to define global variables using scripts

SOLVED
obaid_shirwani
Contributor

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

Think big. You are most likely to end big.
4 REPLIES 4
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?


Helen Kosova
SmartBear Documentation Team Lead
________________________
Did my reply answer your question? Give Kudos or Accept it as a Solution to help others. ⬇️⬇️⬇️

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.

Think big. You are most likely to end big.

Issue resolved. It was a typo error 😞 my bad

Think big. You are most likely to end big.
HKosova
SmartBear Alumni (Retired)

No problem, glad you got it resolved. Smiley Happy


Helen Kosova
SmartBear Documentation Team Lead
________________________
Did my reply answer your question? Give Kudos or Accept it as a Solution to help others. ⬇️⬇️⬇️
cancel
Showing results for 
Search instead for 
Did you mean: