Shruti Gupta
Shruti Gupta

Reputation: 308

Apache JMeter -How do I get variable value in __javaScript function

How do I get variable value in __javaScript function ?

My code:

String[] zoollevelParams = Parameters.split(",");
Random random = new Random();
int zoomValue= Integer.parseInt(zoollevelParams[random.nextInt(10)]);
double lat = 50.669;
double lng = 5.499;
int numTiles = ${__javaScript(Math.pow(2\, "${zoomValue}"))};

This code is unbale to get value of zoomValue in __javaScript function call, how do I get value in this function?

Upvotes: 3

Views: 6636

Answers (2)

Dmitri T
Dmitri T

Reputation: 168157

Don't inline JMeter Variables or Functions into scripts, particular in your case __javaScript function is being executed before zoomValue is getting initialized.

  1. Make sure you use the relevant JSR223 Test Element
  2. Make sure you select groovy from the "Language" dropdown
  3. Make sure you have Cache compiled script if available box ticked
  4. Amend the last line of your code to look like:

    int numTiles = Math.pow(2, zoomValue)
    

Demo:

JMeter Math Pow Groovy

Check out Apache Groovy - Why and How You Should Use It article for more details on using Groovy for scripting in JMeter tests.

Upvotes: 0

Ori Marko
Ori Marko

Reputation: 58862

You can't combine Java and Javascript code and don't need to.

Just keep using Java and take parameter from JMeter variables object vars:

 int numTiles = Math.pow(2, vars.get( "zoomValue"));
  • Note: If you save zoomValue as Double or not a regular String use vars.getObject

Upvotes: 2

Related Questions