-1

I have code like this:

def options = JsonPath.read(prev.getResponseDataAsString(), '$.options')
def randomOption = options.get(RandomUtils.nextInt(0, options.size()))
def code = randomOption.get("code")
vars.put('code1', code)
def values = randomOption.get('values')
def randomValue = values.get(RandomUtils.nextInt(0, values.size())) as 
String
def val = randomValue['value']
vars.put('randomValue', randomValue)
vars.put('ValueF', val). 

In Random Variable i am getting value as [label:Red, value:8] . I need to fetch the value of Value=8

Siddish
  • 97
  • 3
  • 12
  • 1
    `randomvariable.value`, `randomvariable['value']`, `randomvariable.get('value')` should all work. What error do you get? – cfrick Apr 03 '18 at 09:25
  • Got this error! javax.script.ScriptException: javax.script.ScriptException: groovy.lang.MissingMethodException: No signature of method: org.apache.jmeter.threads.JMeterVariables.put() is applicable for argument types: (java.lang.String, [C) values: [ValueF, [label:Red, value:8]] – Siddish Apr 03 '18 at 09:33
  • Your error states a `put` call and there is jmeter involved. You problem seems not just "how can i access a value in a map in groovy", but there is a bigger picture, you left out. Please elaborate. – cfrick Apr 03 '18 at 09:35
  • I have code like this:def options = JsonPath.read(prev.getResponseDataAsString(), '$.options') def randomOption = options.get(RandomUtils.nextInt(0, options.size())) def code = randomOption.get("code") vars.put('code1', code) def values = randomOption.get('values') def randomValue = values.get(RandomUtils.nextInt(0, values.size())) as String def val = randomValue['value'] vars.put('randomValue', randomValue) vars.put('ValueF', val). In Random Variable i am getting value as [label:Red, value:8] . I need to fetch the value of Value=8 – Siddish Apr 03 '18 at 09:41
  • 1
    Please add this to the question. Its quite hard to read in a comment. – cfrick Apr 03 '18 at 09:42
  • 1
    Debug and see what you pass to `vars.put('code1', code)`. This method accepts two Strings, so it looks like `code` is not a String. Your problem is not reproducible. If you are not aware of types that are returned by functions you call, replace all `def` keywords with expected types, so you can get compilation errors with detailed information. – Szymon Stepniak Apr 03 '18 at 10:24
  • Why do you use `as String` when setting `randomValue`? – tim_yates Apr 03 '18 at 13:52

1 Answers1

0

Youre trying to invoke

vars.put('ValueF', [label:Red, value:8])

which is put(String, Map)

JMeterVariables have no such method https://jmeter.apache.org/api/org/apache/jmeter/threads/JMeterVariables.html

you can use putObject() which accepts String as key and Object as value:

vars.putObject('ValueF', val)
Evgeny Smirnov
  • 2,886
  • 1
  • 12
  • 22
  • Object is being saved insted of Value . ie 8 . I need value=8 on my variable 'ValueF' – Siddish Apr 03 '18 at 10:53
  • ? Any inputs on my comments ? – Siddish Apr 03 '18 at 11:00
  • You can access it something like: def objct = [value:8];vars.putObject('ValueF', objct);println(vars.getObject('ValueF').value). Or you can put only 8 to your vars like vars.put('ValueF', objct.value) – Evgeny Smirnov Apr 03 '18 at 13:19
  • Could it be more specific , because i cannot use def objct = [value:8] w.r.t above code in Question! – Siddish Apr 03 '18 at 13:22
  • vars.putObject('ValueF', [value: val.value]) – Evgeny Smirnov Apr 03 '18 at 13:49
  • Dint worked. Got this error again javax.script.ScriptException: javax.script.ScriptException: groovy.lang.MissingPropertyException: No such property: value for class: [C – Siddish Apr 04 '18 at 05:11
  • Can you provide output println(val.dump()) ? – Evgeny Smirnov Apr 04 '18 at 07:17
  • ERROR o.a.j.p.j.s.JSR223Sampler: Problem in JSR223 script JSR223 Sampler, message: javax.script.ScriptException: javax.script.ScriptException: groovy.lang.MissingPropertyException: No such property: value for class: [C javax.script.ScriptException: javax.script.ScriptException: groovy.lang.MissingPropertyException: No such property: value for class: [C at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:158) ~[groovy-all-2.4.12.jar:2.4.12] at javax.script.AbstractScriptEngine.eval(Unknown Source) ~[?:1.8.0_161] – Siddish Apr 04 '18 at 12:49
  • any luck on the above? – Siddish Apr 05 '18 at 06:54
  • Provide output of following statement println(val.dump()) or println(val.getClass()) please. Put it before line when you calling val.val – Evgeny Smirnov Apr 05 '18 at 09:37
  • 2018-04-05 16:58:44,566 ERROR o.a.j.p.j.s.JSR223Sampler: Problem in JSR223 script JSR223 Sampler, message: javax.script.ScriptException: javax.script.ScriptException: groovy.lang.MissingPropertyException: No such property: value for class: [C javax.script.ScriptException: javax.script.ScriptException: groovy.lang.MissingPropertyException: No such property: value for class: [C . This is the error i got – Siddish Apr 05 '18 at 11:29