How to define global variables using scripts
SOLVED- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
Solved! Go to Solution.
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Assuming
Log.Message("03. Global value of 'myGlobalVariable': " + loginArray);
// should be myGlobalVariable
is just a typo, your code works fine:
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. ⬇️⬇️⬇️
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Issue resolved. It was a typo error 😞 my bad
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
No problem, glad you got it resolved.
Helen Kosova
SmartBear Documentation Team Lead
________________________
Did my reply answer your question? Give Kudos or Accept it as a Solution to help others. ⬇️⬇️⬇️
