Global Variables Usage

I have a script 1 where I have following lines of code

Global gTest
put "Hello Global" into gTest

I have script 2 where I am referring the global variable like following


put "testing"
put global gTest
put "done"
I get an empty string in the results below

testing

done
How ever if I have set the global variable in a handler in Script 1

to setVariable
Global gTest
put "Hello Global" into gTest
end setVariable

If I have code in Script 2 and execute, it does print the value

Script1.setVariable
put global gTest

Documentation about this feature is not very clear to me and do not understand why I have to put in a handler to work.

Thanks
Jag

Comments

  • JonathanOSXJonathanOSX ForumAdmin admin
    Your first scenario should work as long as it's all done during a single execution. EG:
    Global gTest 
    put "Hello Global" into gTest 
    
    script2 // Call script 2 will output "Global gTest"
    
    Global variables do not persist from one run to the next. So if you call script2 after the first script finishes then it'll be empty. You can use "universal" variables if you want them to persist for the duration of you Eggplant session (they will go away when you quit Eggplant).
  • JagJag Member
    Thanks, Jonathan! Let me state the problem we want solution for

    We want to declare the Images' path in variables in a common script and use them in separate script.
    script 1
    like set global applImages to "\Main\Sub\Sub1\"
    script 2
    set myImage to global appImages&"\myImage"
  • JonathanOSXJonathanOSX ForumAdmin admin
    That is a very common usage and it should work fine as long as you run both scripts in the same session and either declare the variable as global at the beginning of the handler:
    global applImages
    or always refer to it with the word global in front:
    set myImage to global appImages & "\myImage".
  • JagJag Member
    Thanks! As I understand that these variables will not be set unless that script is run. So we cannot have a helper suite as library to hold these variables and refer these variablesto get the values set in the helper library.

    Jag
  • JonathanOSXJonathanOSX ForumAdmin admin
    You can certainly chain calls of scripts together in Eggplant so you CAN definitely setup a variable in a helper script or helper suite and THEN call your primary scripts.

    Here is a simple example suite showing access to global variables.
Sign In or Register to comment.