Previous Topic: Custom Test Step ExecutionNext Topic: Execute Script (JSR-223) Step


Java Script Step (deprecated)

Note: The Java Script step has been deprecated. Use the Execute Script (JSR-223) step instead.

The Java Script step gives you the flexibility of writing and executing a Java script to perform some function or procedure. Your script is executed using the BeanShell interpreter. You have access to all the properties in the test case, including built-in objects.

Prerequisites: Some knowledge of BeanShell. For more information about BeanShell, see http://www.beanshell.org/.

The step includes a script editor. Double-clicking an item in the Available Objects list pastes that variable name into the editor.

The last value that is exposed in the script is saved as the response of this step.

The following graphic shows the script editor. The script contains the following statements:

The Test button lets you test the script. You see the result from executing the script, or an error message describing the error that occurred.

DevTest property name syntax is flexible and can include spaces. The property names that are not valid Java identifiers are converted for use in this step. An underscore (_) replaces any invalid characters.

If you use properties {{exampleprop}} in a script, DevTest substitutes the properties for the actual property values at run time before it runs the script.

Properties with "." in their names are imported into the script environment with "." replacing "_". So {{foo.bar}} in a script is the same as foo_bar.

You can produce a log event inside a script step or assertion. A testExec object is useful. To produce a log event, code the following instead of using the log4j logger. The testExec.log() object causes an actual event to be raised and you can see it in the ITR.

testExec.log("Got here"); 

A test case run has only one scripting instance. If there are multiple Java scripting steps, whether in the same test case or in subprocesses, DevTest uses the same instance to run the entire test case.

By default, variables are global to the instance. This behavior extends to subprocesses.

If you want the scope of a variable to be local, place the code inside an opening curly brace and a closing curly brace. For example:

{
     String var= "local";
     return var;
}

Parameter names for subprocesses are treated as global variables.