JMeter __javaScript function reference: syntax, parameters, and practical examples for parameterizing test plans with dynamic values.
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:
log- the Logger for the functionctx- JMeterContext objectvars- JMeterVariables objectthreadName- String containing the current thread namesampler- current Sampler object (if any)sampleResult- previous SampleResult object (if any)props- JMeterProperties (classjava.util.Properties) object
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()
| Name | Required | Description |
|---|---|---|
| Expression | Yes | The 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 Name | No | A reference name for reusing the value computed by this function. |
Examples:
\${__javaScript(new Date())}will return Sat Jan 09 2016 16:22:15 GMT+0100 (CET)
\${__javaScript(new Date(),MYDATE)}will return Sat Jan 09 2016 16:22:15 GMT+0100 (CET) and store it under variable MYDATE
\${__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
\${__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
\${__javaScript("\${VAR}"=="abcd",MYRESULT)}will compare the value of VAR variable with abcd, return true or false and store the result in MYRESULT