This topic explains how to create and apply an Extension tool. The tool performs custom operations written in Java, JavaScript, OpenJDK Nashorn, Groovy, Jython, or other scripting engines that implement the JSR 223 "Scripting for the Java Platform" specification.
Sections include:
Understanding the Scripting/Extension Tool
The scripts may accept zero, one, or two arguments. The source of the output can be a derived from a client request, a server response, the output of another tool, an element of a rule, or it can be user defined beforehand. This is useful if you want to perform application-specific checks that cannot be expressed with a rule (for example, if you want to check if a given output matches a record in a database). Or you can design the script to perform any specific function that would be helpful to you.
If your method accepts zero arguments, then it does not matter what file is selected when it is run. If the method accepts one argument, then the input will be taken from the file selected or the tool to which it is attached. If the method accepts two arguments, then the first will be taken from the file or preceding tool and the second will take contextual information about the file. For more information on the types of context arguments that can be applied, go to Parasoft > Help > Scripting API. To do this, create a Jython or JavaScript script, then add it to the <INSTALL_DIR>/plugins/com.parasoft.ptest.libs.web_<VERSION>/root/startup
directory.
Creating a Custom Script/Extension Tool
The following procedure describes how to add an Extension tool to the toolbar. You can also create custom methods in a test suite (in SOAtest) or Responder suite (in Virtualize) by adding an Extension tool, then completing the same parameters in the Extension parameters panel.
To add a custom script/Extension tool to SOAtest or Virtualize:
The tool configuration panel will open on the right.
- Give your method a name in the Name field.
If a return value for this tool indicates the tool’s success, enable Exit code indicates success. If this option is not enabled, the return value of the method is ignored regardless of whether the tool succeeded or failed.
- Choose the language that your method is or will be written in from the Language menu.
- Define the script to be implemented in the large text field.
- For Java methods, specify the appropriate class in the Class field. Note that the class you choose must be on your classpath (you can click the Modify Classpath link then specify it in the displayed Preferences page). Click Reload Class if you want to reload the class after modifying and compiling the Java file.
For other scripts, you can use an existing file as the source of code for your method or you can create the method in the UI.
- To use an existing file, enable File and click Browse. Navigate to the file and select it, then click OK.
To create the method in the UI from scratch, enable Text and enter your code in the associated text window.
To check that the specified script is legal and runnable, right-click the File or Text field (whichever one you used to specify your script) and choose Evaluate. Any problems found will be reported.
- Click Evaluate to check that the script will run (does not contain syntax errors).
- In the Error message field, specify what error message should be reported if the tool fails.
- Select the appropriate argument from the Method menu at the bottom of the panel. This list will be composed of any definitions contained in your script. Since a script can contain multiple arguments, you can select the one that you want to use in this method.
Using Variables in Your Script?
If your script returns content that contains ${}
format variables such as ${value}
, those variables will be resolved before any attached output tools process the content. If you do not want those variables to be resolved, be sure to escape the variables in your script.
For example, assume that your original script has:
def handler(input, context) { def output = '{ "field": "${value}" }'; com.parasoft.api.Application.showMessage(output); return output; }
To prevent ${value}
from being processed, you would modify it to:
def handler(input, context) { def output = '{ "field": "${value}" }'; output = output.replace('${', '\\${'); com.parasoft.api.Application.showMessage(output); return output }
Additional Scripting Resources
For an overview of issues related to the scripting feature and its various applications, see Extensibility and Scripting Basics.