How to pass variables accross scripts ?

How can we pass variables accross scripts

Though Sensetalk reference manual says we can use the ‘universal’ but somehow I am not able to get the desired result i.e pass the value in a variable accross the script. The ‘global’ keyword works only within one script and not accross scripts.

Can someone help me out on this?

Are you declaring the variable in both scripts? I’ve found that I have to say

universal myVar

in all scripts that need to access that information.

Doug posted some great information about them on this thread that I started about universals.

Allen is correct. To use either global or universal variables, you need to declare the variable (and its type) in each script that you expect to use it in. I’d suggest that you should use global variables rather than universal variables – universal variables will retain their values even across script runs, which can have some unintended side effects, especially if you expect them to occasionally be empty. Global variables are accessible by any script that declares them, but they are reinitialized each time you click Run.

I should also point out that most of the time, the best way to pass variables is as parameters (or arguments). For example, say you create a script to perform some procedure that you are going to need to repeat a lot, and that script needs to know what image(s) it’s going to be targeting. You would start that script with declaration similar to the following:

params image1, image2

Then, assuming your script is called DoTheThing, you will call it like this:

DoTheThing "imageName1", "imageName2"

And within the DoTheThing script, you can refer to the passed images using the names from the params declaration:

waitfor 8, image1
click image2

You can also declare functions, commands, and “to” handlers with parameters, for example:

function myFunction someImage, someString

It’s even possible to pass values by reference so that they can be modified by the handler that receives them, but that’s a little beyond the scope of this discussion.

In general, you should use universal variables only if you want to be able to set some value the first time you run the script, and then just reuse that value on each subsequent run of the script or during the execution of other scripts (that are run separately from the run in which the universal’s value is set). Note that universal variables persist only as long as the current Eggplant session – if you quit Eggplant the values will not be retained.

You should use global variables if you just want different scripts or handlers within a single run to be able to read and write the values of those variables. This is the advantage of global variables, but it is also a disadvantage, because you need to be aware of which handlers will be writing to the variable and how that may affect handlers further down the chain.

If you just want a script/handler to be able to use the value stored in a variable and not be able to modify that variable’s value in the calling script, then you should design your code so that it is passed as a parameter. You can modify the value of a passed in parameter and it will not change the value in the calling script (unless you passed it by reference, but that’s more than I want to try to cover in this post).

For more information on using scripts, functions, and other handlers, please see the article titled “Writing Reusable Code: Using Functions and Handlers” on page 40 of the Using Eggplant manual.

Regards,
Matt

The only thing that I really use a universal for is the Product Under Test (we test about 8 different apps) and the current version. I have a script that all it does is set those variables (kinda like a versioning header) and those get passed as parameters into report generation.