3

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?

2 Answers 2

2

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
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for answer, I tried your solution but I got an exception - org.apache.jorphan.util.JMeterException: Error invoking bsh method: eval Sourced file: inline evaluation of: ``String[] zoollevelParams = Parameters.split(","); Random random = new Random(); . . . '' : Typed variable declaration : Error in method invocation: Static method pow( int, null ) not found in class'java.lang.Math'
It means zoomValue has no value. If you want more help share you test plan how zoomValue is defined
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.