Is there a way to pass in a variable when calling a script?

Is there a way to pass in a variable when calling a script? Specifically, I would like to be able to send a machine name in to a script and then have the script use that machine name as a variable. For example:

In script A I call script B like this:

run "B.script" machineName

Then in script B, somehow assigne the passed in value “machineName” to a variable called machineName (I am guessing here…) and executing something like the following:

set the searchRectangle to (20, 100, 200, 400)
if ImageFound(15, Text: machineName)
	Click (text: machineName)
	Log "Successfully selected the machine"
	set the searchRectangle to fullscreen
else
	LogError "Error: Specified machine was not found."
	return
end if

Is something like this possible?

Hello,

In my response to your other question, I talk about passing parameters:
http://www.testplant.com/phpBB2/viewtopic.php?p=8816#8816

If that description does not explain it in enough detail, you can also read in the documentation:
“Receiving Passed Parameters” in the SenseTalk Reference Manual
“Parameters and Results” in the SenseTalk Reference Manual

You can find the manuals on the download page:

-Elizabeth

Yes, functions, on’s, and scripts can pass variables as parameters. Elizabeth did put it in her post on you’re previous thread, but she didn’t quite explain it as she states in this thread. Yet, if you read her code snippets in the other thread you can see that the beginning of the snippet shows “Params”. Those params are where you’re “variables” that you call with the script go.

SO basically you’ll call the script like this:

run "B.script" Machine1

Where “Machine1” is the actual name of the machine, so when the script launches it reads the 1st variable as Parameter 1 and so on.

In the script you’d put this at the top:

Params MachineName 

and then whenever you want to call “MachineName” you’ll get “Machine1” as the value.

This again works for functions, on’s as well. Except one difference.

to call a function with param’s, you function first has to support params, to do this when you create you function, you just put some params after it, as so:

function Whatever1 Param1, Param2
on Whatever2 Param1, Param2

and in the script you’d just call the function or the on with the variables you want to pass.

For Function:

Log Whatever1() Blue, Orange

For On:

Whatever2() Red, Green

So whenever you call Param1 or Param2 for the function, you’ll get Blue and Orange. When you call Param1 or Param2 for the on, you’ll get Red and Green.

That’s as simple as I can put it. Elizabeth or Matt feel free to chime in if I’m wrong in any of the above.

[quote=“ne0hisda0ne”]…if you read her code snippets in the other thread you can see that the beginning of the snippet shows “Params”. Those params are where you’re “variables” that you call with the script go.

SO basically you’ll call the script like this:

run "B.script" Machine1

Where “Machine1” is the actual name of the machine, so when the script launches it reads the 1st variable as Parameter 1 and so on.

In the script you’d put this at the top:

Params MachineName 

and then whenever you want to call “MachineName” you’ll get “Machine1” as the value.[/quote]

This is what I figured based on the other note and so I wrote a simple test where one script calls another exactly as described above. However I had a line in front of the Params line that logged what I was doing. When I ran the first script, I got the following error:

18-May-12 17:21:24 FAILURE STUnknownMessage ERROR: ‘params’ was called as a Command, but is only available as a Function
Execution Time 0:00:00 test.script

Just FYI for anyone else who reads this thread, the Params line has to be the first (other then comments) thing in the script. When that is the case it works as expected.

Thanks to both of you who replied. :smiley:

A few closing comments, if I may:

Yes, you’re exactly right: the params declaration must be the first (non-comment) line in the script.

Writing small test scripts and experimenting (as you did) is an excellent way of learning how any language really works, regardless of what its documentation may say (but please let us know if the SenseTalk docs describe anything differently than the way they actually work!).

Finally, a suggestion on style: the ‘run’ command is rarely needed, and scripts are generally clearer and less cluttered without it. Instead of:

run "B.script" machineName

you could simply do this:

B machineName

The run command is only needed if the script to be run has a name that includes spaces or other special characters that aren’t allowed as part of a command name, or if you need to supply the full path name of a script in some non-standard location.

Actually, we are currently calling many scripts from a master script using the RunWithNewResults command. We then analyze the result per script run and incorporate them in to a results file.

What I have found is that the following line in my master script (scripta.script):

RunWithNewResults "c:\eggplant\General.suite\Scripts\scriptb.script" machine1

… causes an error:

2012-05-21 14:35:51.788 -0600	START		Running scripta.script
2012-05-21 14:35:53.758 -0600	FAILURE	Screen_Error.png	SenseTalk Compiler Exception in scripta:
Syntax Error at line 131: Syntax Error - can't understand "machine1" at or near character 7149
Execution Time 0:00:00 scripta.script

The only way I can get that to run in to replace RunWithNewResults with just Run.

Have I found a bug or am I just trying to something that is not allowed?

Hello,

So when you call a machine by name like that you will need to have a comma after the file path.

RunWithNewResults "filepath", machine1

Something that can be really helpful in a situation like this is setting your colorization to on. As you can see in the attached screenshot, when the statement is formatted incorrectly with the colorization turned on, it does not colorize, whereas it colorizes when it is formatted correctly. This can draw your attention to any syntactical mistakes you may make while writing your script, before you run it and it throws an error.
You can set the colorization preferences under eggPlant Preferences>Script>Colorization. Just make sure “Enable syntax colorization” is selected. Having “Update colors continuously while typing” turned on is also very useful.

-Elizabeth

Thanks Elizabeth. “Update colors continuously while typing” is something I had been wishing for, and I apparently missed it the first time through preferences. :smiley:

I am dealing with a similar situation.
In one of my scripts I generate a random value for a text field and store it into a universal variable.
What I’d like to do now is pass the stored value to the next script which will use it to call other things.

I also came up with using

Put (ReadText (“text”)) into shortName

but cannot pass the value to the next script. Is there an easy way to do this without defining params?

Thanks in advance.

That would be called a global variable.

Put (ReadText (“text”)) into global shortName

Then call it in the other script like this:

Log global shortName

Thank you!
Actually it worked with the universal variable just fine :slight_smile:

I have this issue and need help. I need to pass a path to the eggplant script. Inside the script, i need to navigate to that path (by doing TypeText “cd toThePaht”, Return. So, Jenkins will call my script like this:

runscript /x/y/z/eggplant/eggplant.script -user jenkins -password rootroot ${test_dir}

Inside my script, I would save test_dir like this:

Params test_dir

– I need to navigate to test_dir
TypeText “cd test_dir”, Return <==== this does not work as eggplant does not regconize test_dir. How do i dereference test_dir in TypeText command?

TypeText will treat anything in quotes as a literal and anything not in quotes as a variable. TypeText also allows you to string together :wink: literals and variables by separating them with commas. A common example would be TypeText controlKey,"c" -- Copy command
For your example, it should be as straightforward as TypeText "cd",test_dir
Hope this helps,
Dave