Skip to content

__javaScript

JMeter __javaScript function reference: syntax, parameters, and practical examples for parameterizing test plans with dynamic values.

Difficulty
intermediate
Guide type
reference
Estimated read time
2 min read
Last verified version
Verified JMeter 5.6

Also documented in context in the full Functions and Variables reference.

The javaScript function executes a piece of JavaScript (not Java!) code and returns its value

The JMeter Javascript function calls a standalone JavaScript interpreter. Javascript is used as a scripting language, so you can do calculations etc.

For Nashorn Engine, please see Java Platform, Standard Edition Nashorn Userโ€™s Guide.

For Rhino engine, please see Mozilla Rhino Overview

The following variables are made available to the script:

Rhinoscript allows access to static methods via its Packages object. See the Scripting Java documentation. For example one can access the JMeterContextService static methods thus: Java.type("org.apache.jmeter.threads.JMeterContextService").getTotalThreads()

NameRequiredDescription
ExpressionYesThe JavaScript expression to be executed. For example: - new Date() - return the current date and time - Math.floor(Math.random()*(\${maxRandom}+1)) - a random number between 0 and the variable maxRandom - \${minRandom}+Math.floor(Math.random()*(\${maxRandom}-\${minRandom}+1)) - a random number between the variables minRandom and maxRandom - "\${VAR}"=="abcd"
Variable NameNoA reference name for reusing the value computed by this function.

Examples:

Terminal window
\${__javaScript(new Date())}

will return Sat Jan 09 2016 16:22:15 GMT+0100 (CET)

Terminal window
\${__javaScript(new Date(),MYDATE)}

will return Sat Jan 09 2016 16:22:15 GMT+0100 (CET) and store it under variable MYDATE

Terminal window
\${__javaScript(Math.floor(Math.random()*(\${maxRandom}+1)),MYRESULT)}

will use maxRandom variable, return a random value between 0 and maxRandom and store it in MYRESULT

Terminal window
\${__javaScript(\${minRandom}+Math.floor(Math.random()*(\${maxRandom}-\${minRandom}+1)),MYRESULT)}

will use maxRandom and minRandom variables, return a random value between maxRandom and minRandom and store it under variable MYRESULT

Terminal window
\${__javaScript("\${VAR}"=="abcd",MYRESULT)}

will compare the value of VAR variable with abcd, return true or false and store the result in MYRESULT

On this page