Reusable library scripts in automation scripts

A library script is a reusable piece of programming logic that automation scripts can invoke from within the body of their code. Creating library scripts can save time and effort. Library scripts are stored in the database in the AUTOSCRIPT table.

An example of a basic script is multiplication. You can create a library script that is named MULTIPLY:

z=x*y

You can then create a script that uses the MULTIPLY library script and logs the result:

service.log("I want to multiply 2 numbers and log the result")
from java.util import HashMap
a=3
b=2
ctx = HashMap()
ctx.put("x",a)
ctx.put("y",b)
service.invokeScript("MULTIPLY",ctx)
service.log("the result is "+str(ctx.get("z")))

The implicit variable in the service line is used to invoke the MULTIPLY library script: service.invokeScript("MULTIPLY",ctx). When the MULTIPLY library script is invoked, the variables that are required by the script are passed into the script, which is done by the ctx lines and argument.

When you use a library script in an automation script, you must then define a Java™ HashMap object, which is done in the example in the ctx = HashMap() line. You place the variable names and values into the HashMap object. You must know the required variables for the library script, their names, and their data types, which in the example is accomplished in the ctx.put("x",a) line and the ctx.put("y",b) line.

If you want to receive output from the library script, then you must also use the name of the response variable in the automation script. The example script uses the response variable in the service.log("the result is "+str(ctx.get("z"))) line.



Feedback