Search code examples
javagroovyjmeterjsr233

Problem in JSR223 script JSR223 Sampler groovy.lang.MissingPropertyException: No such property: length for class: java.lang.String


I'm trying to mask the user's password.

This is the piece of code I used in JSR223 Sampler (for testing purposes. I have to place this code in the JSR223 Pre-processor to be used to login to the website). I'm not sure if there are configurations that I need to setup in the properties file or there are jars that I still need to donwload to make this work.

String pw = "Password_1";
String pw2 = "";

for (var i=0; i < pw.length; i++)
{pw2 += "*";}

println("This is the user's password: "+pw);
println("This is the user's masked password: "+pw2);

After running the code, I got the error below:

2024-02-22 17:09:31,973 INFO o.a.j.t.JMeterThread: Thread started: Thread Group 1-1
2024-02-22 17:09:32,005 ERROR o.a.j.p.j.s.JSR223Sampler: Problem in JSR223 script JSR223 Sampler, message: javax.script.ScriptException: groovy.lang.MissingPropertyException: No such property: length for class: java.lang.String
javax.script.ScriptException: groovy.lang.MissingPropertyException: No such property: length for class: java.lang.String
    at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:320) ~[groovy-jsr223-3.0.20.jar:3.0.20]
    at org.codehaus.groovy.jsr223.GroovyCompiledScript.eval(GroovyCompiledScript.java:71) ~[groovy-jsr223-3.0.20.jar:3.0.20]
    at java.scripting/javax.script.CompiledScript.eval(CompiledScript.java:93) ~[java.scripting:?]
    at org.apache.jmeter.util.JSR223TestElement.processFileOrScript(JSR223TestElement.java:230) ~[ApacheJMeter_core.jar:5.6.3]
    at org.apache.jmeter.protocol.java.sampler.JSR223Sampler.sample(JSR223Sampler.java:72) ~[ApacheJMeter_java.jar:5.6.3]
    at org.apache.jmeter.threads.JMeterThread.doSampling(JMeterThread.java:651) ~[ApacheJMeter_core.jar:5.6.3]
    at org.apache.jmeter.threads.JMeterThread.executeSamplePackage(JMeterThread.java:570) ~[ApacheJMeter_core.jar:5.6.3]
    at org.apache.jmeter.threads.JMeterThread.processSampler(JMeterThread.java:501) ~[ApacheJMeter_core.jar:5.6.3]
    at org.apache.jmeter.threads.JMeterThread.run(JMeterThread.java:268) ~[ApacheJMeter_core.jar:5.6.3]
    at java.base/java.lang.Thread.run(Thread.java:1583) [?:?]
Caused by: groovy.lang.MissingPropertyException: No such property: length for class: java.lang.String
    at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:65) ~[groovy-3.0.20.jar:3.0.20]
    at org.codehaus.groovy.runtime.callsite.GetEffectivePojoPropertySite.getProperty(GetEffectivePojoPropertySite.java:65) ~[groovy-3.0.20.jar:3.0.20]
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callGetProperty(AbstractCallSite.java:329) ~[groovy-3.0.20.jar:3.0.20]
    at Script2.run(Script2.groovy:45) ~[?:?]
    at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:317) ~[groovy-jsr223-3.0.20.jar:3.0.20]
    ... 9 more
2024-02-22 17:09:32,005 INFO o.a.j.t.JMeterThread: Thread is done: Thread Group 1-1
2024-02-22 17:09:32,005 INFO o.a.j.t.JMeterThread: Thread finished: Thread Group 1-1
2024-02-22 17:09:32,005 INFO o.a.j.e.StandardJMeterEngine: Notifying test listeners of end of test
2024-02-22 17:09:32,005 INFO o.a.j.g.u.JMeterMenuBar: setRunning(false, *local*)

I'm trying to make the piece of code work because I will need to put that code in JSR223 Pre-processor.


Solution

  • There is no such field like length

    there is a function length() to wit you need to add parentheses.

    for (var i=0; i < pw.length(); i++) {pw2 += "*";}
    

    More information: