Agilitest is known for its broad coverage of various recent technologies in software development, and generally allows you to automate just about anything with a few clicks and graphically.
In some cases, however, it is useful to call external code to perform an environment-specific operation.
Agilitest can call a piece of Java code, passing in input variables and retrieving output parameters.
Of course, we recommend all the usual precautions when using Java external code, because while it can do everything, it can also have an unwanted impact on the SUT (System Under Test).
In terms of portability, the .ats code produced by Agilitest is transformed into Java for execution. You can therefore assume that the Java code you write will be portable to the various platforms supported by ATS.
Create a Java script
To begin with, you will generate the stub of your java code in Agilitest.
To do that, change the view to “Java ATS scripts” in the project explorer,
Click on the button to “Create a Java ATS script”.
You can also create a Java script by clicking on “New component”, at the top left of the editor.
The editor opens a popup asking for the script name. Enter your script name and click "OK".
The editor opens a Java code editing window and, after the necessary and useful declarations, displays the function to be called: testMain().
It is possible to pass parameters by value to your Java script.
To do this, click on the "Call script" action in your script.
Then, click on "Add parameter”.
Next, you can enter the values you want to pass to your script, and you can use all the possibilities offered by the Agilitest editor: use of variables, "hard" values, iterations on a .csv file…
To retrieve them from the Java code, you have the getParameter(int) function, which takes an integer corresponding to the index of the variable in the script call: getParameter(0) will return the first value passed in the parameters.
Results are retrieved symmetrically to the script call.
This requires the declaration of variables in the Agilitest editor, which will be of a type that cannot be modified (by the user).
To declare return variables, proceed as follows:
Declare a new variable in the variable management menu on the left of the editor.
Name the variable and drop it directly in the call script action.
Agilitest will change the type of the variable to make it non-modifiable by the user (indicated by the orange coloured variable) and mark it as a “Returned variable” for the called Java script.
The variable can be used in your script for future operations.
To define a return value on the Java side, you have the returnValues() function, which takes all the values you wish to return:
returnValues("value1", "retvalue2", param1) ;
Obviously, the number of values returned must correspond to the number of variables declared in the Agilitest editor.
If your test includes a Java script, it will be necessary to compile it before running the test. During compilation, any syntax errors will be highlighted.
Once your project has been compiled, you can run your test and Java actions as if they were standard Agilitest actions: step by step (action by action) and with the same execution mode, each Java action being executed in one go.
Before running your test, once the Java compilation has been completed, you must close all open channels to reload the compiled Java modifications.
If your Java code is a little complex, and you want to debug it, you can use step-by-step debugging provided you have assigned a Java debugging port in Agilitest and have an IDE such as eclipse or intelliJ.