0
votes

I'm trying to open a file with a JSR223 Sampler using Groovy.

The file is in the same directory as the .jmx file. The Sampler runs first in the test plan, so no other part of the plan should interfere with it.

Script

import org.apache.jmeter.services.FileServer;

String path = FileServer.getFileServer().getBaseDir() + "\\requestBody.txt"

String file = new File(path).getText('UTF-8')

In the log, I'm getting the exception:

2021-02-18 15:46:17,708 ERROR o.a.j.p.j.s.JSR223Sampler: Problem in JSR223 script Load SOAP Request Config, message: javax.script.ScriptException: javax.script.ScriptException: java.io.FileNotFoundException: C:\Users\MatthiasWalk\Documents\Workspace\JMeter\requestBody.txt (The system cannot find the file specified)
javax.script.ScriptException: javax.script.ScriptException: java.io.FileNotFoundException: C:\Users\MatthiasWalk\Documents\Workspace\JMeter\requestBody.txt (The system cannot find the file specified)
    at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:158) ~[groovy-jsr223-3.0.7.jar:3.0.7]
    at javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:233) ~[java.scripting:?]
    at org.apache.jmeter.util.JSR223TestElement.processFileOrScript(JSR223TestElement.java:219) ~[ApacheJMeter_core.jar:5.4.1]
    at org.apache.jmeter.protocol.java.sampler.JSR223Sampler.sample(JSR223Sampler.java:72) [ApacheJMeter_java.jar:5.4.1]
    at org.apache.jmeter.threads.JMeterThread.doSampling(JMeterThread.java:638) [ApacheJMeter_core.jar:5.4.1]
    at org.apache.jmeter.threads.JMeterThread.executeSamplePackage(JMeterThread.java:558) [ApacheJMeter_core.jar:5.4.1]
    at org.apache.jmeter.threads.JMeterThread.processSampler(JMeterThread.java:489) [ApacheJMeter_core.jar:5.4.1]
    at org.apache.jmeter.threads.JMeterThread.run(JMeterThread.java:256) [ApacheJMeter_core.jar:5.4.1]
    at java.lang.Thread.run(Thread.java:834) [?:?]
Caused by: javax.script.ScriptException: java.io.FileNotFoundException: C:\Users\MatthiasWalk\Documents\Workspace\JMeter\requestBody.txt (The system cannot find the file specified)
    at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:320) ~[groovy-jsr223-3.0.7.jar:3.0.7]
    at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:155) ~[groovy-jsr223-3.0.7.jar:3.0.7]
    ... 8 more
Caused by: java.io.FileNotFoundException: C:\Users\MatthiasWalk\Documents\Workspace\JMeter\requestBody.txt (The system cannot find the file specified)
    at java.io.FileInputStream.open0(Native Method) ~[?:?]
    at java.io.FileInputStream.open(FileInputStream.java:219) ~[?:?]
    at java.io.FileInputStream.<init>(FileInputStream.java:157) ~[?:?]
    at org.codehaus.groovy.runtime.ResourceGroovyMethods.newReader(ResourceGroovyMethods.java:1797) ~[groovy-3.0.7.jar:3.0.7]
    at org.codehaus.groovy.runtime.ResourceGroovyMethods.getText(ResourceGroovyMethods.java:579) ~[groovy-3.0.7.jar:3.0.7]
    at org.codehaus.groovy.runtime.dgm$1042.invoke(Unknown Source) ~[groovy-3.0.7.jar:3.0.7]
    at org.codehaus.groovy.runtime.callsite.PojoMetaMethodSite$PojoMetaMethodSiteNoUnwrapNoCoerce.invoke(PojoMetaMethodSite.java:247) ~[groovy-3.0.7.jar:3.0.7]
    at org.codehaus.groovy.runtime.callsite.PojoMetaMethodSite.call(PojoMetaMethodSite.java:56) ~[groovy-3.0.7.jar:3.0.7]
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:47) ~[groovy-3.0.7.jar:3.0.7]
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:125) ~[groovy-3.0.7.jar:3.0.7]
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:139) ~[groovy-3.0.7.jar:3.0.7]
    at Script8.run(Script8.groovy:5) ~[?:?]
    at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:317) ~[groovy-jsr223-3.0.7.jar:3.0.7]
    at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:155) ~[groovy-jsr223-3.0.7.jar:3.0.7]
    ... 8 more

The path seems to be alright, the file exists and I checked it for trailing/leading whitespaces.

What am I missing?

1

1 Answers

0
votes

I cannot reproduce your issue for the file which exists and my user has read permissions for this file:

enter image description here

So I would recommend double-checking the file path and your user access details.

Also

  • if you need to read the file from the current folder there is no need to use full path, file name will be sufficient

  • reading a file into a string can be done without having to write a single line of code, JMeter provides __FileToString() function:

    enter image description here

See Apache JMeter Functions - An Introduction article to get familiarized with JMeter Functions concept.