0
votes

I have a jMeter JSR223PostProcessor script which parses and validates response.

        <JSR223PostProcessor guiclass="TestBeanGUI" testclass="JSR223PostProcessor" testname="CitiesAssertion" enabled="true">
            <stringProp name="TestPlan.comments">Asserts that actual cities are equal to expected cities</stringProp>
            <stringProp name="cacheKey">true</stringProp>
            <stringProp name="filename"></stringProp>
            <stringProp name="parameters"></stringProp>
            <stringProp name="script">

expectedCities = [&quot;Prague&quot;, &quot;Brno&quot;, &quot;Ostrava&quot;, &quot;Berlin&quot;, &quot;Minsk&quot;, &quot;Warsaw&quot;] as Set

responseData = prev.getResponseData();
responseDataParsedJson = new groovy.json.JsonSlurper().parse(responseData);
actualCities = responseDataParsedJson as Set

log.info(&quot;actualCities: {}&quot;, actualCities);

// Assert may not work.
assert actualCities == expectedCities;

if (actualCities != expectedCities) {
    AssertionResult.setFailure(true)
    AssertionResult.setFailureMessage(&quot;actualCities are not equal to expectedCities. actualCities:  $actualCities, expectedCountries: $expectedCities&quot;)
}

            </stringProp>
            <stringProp name="scriptLanguage">groovy</stringProp>
        </JSR223PostProcessor>

I want to move the code of this script into a separate file and call it from jMeter step.

Now, it says it cannot find the script file.

020-08-10 14:31:50,969 ERROR o.a.j.e.JSR223PostProcessor: Problem in JSR223 script, CitiesAssertion
javax.script.ScriptException: Script file 'C:\Users\johndoe\Desktop\apache-jmeter-5.3\bin\.\groovy\CitiesAssertion.groovy' does not exist or is unreadable for element:CitiesAssertion
    at org.apache.jmeter.util.JSR223TestElement.processFileOrScript(JSR223TestElement.java:205) ~[ApacheJMeter_core.jar:5.3]
    at org.apache.jmeter.extractor.JSR223PostProcessor.process(JSR223PostProcessor.java:45) [ApacheJMeter_components.jar:5.3]
    at org.apache.jmeter.threads.JMeterThread.runPostProcessors(JMeterThread.java:940) [ApacheJMeter_core.jar:5.3]
    at org.apache.jmeter.threads.JMeterThread.executeSamplePackage(JMeterThread.java:572) [ApacheJMeter_core.jar:5.3]
    at org.apache.jmeter.threads.JMeterThread.processSampler(JMeterThread.java:489) [ApacheJMeter_core.jar:5.3]
    at org.apache.jmeter.threads.JMeterThread.run(JMeterThread.java:256) [ApacheJMeter_core.jar:5.3]
    at java.lang.Thread.run(Thread.java:834) [?:?]

When I open this script via jMeter UI, it will make script file / file name absolute path, which I do not want.

cities_assertion


How to get the absolute folder location of a Jmeter .jmx project file from within itself?

It says how to find script location in script. I thought that there would be a variable or parameter that points to the script location. I tried to have a debug element with jMeter properties=true and jMeter variables=true, etc, but it did not help (I did not find a variable or param pointing out to the location of test folder).


Some idea

It turns out https://stackoverflow.com/a/31164646/1839360 that I can have a script at the beginning which resolves the location of jmx file and stores into into a variable. Then I can use this variable as a starting point of script location. It looks a bit complex though: I want to run jmeter from GUI and from command line, therefore I have to resolve GUI mode or non GUI mode.

2

2 Answers

0
votes

According to the answer, it works with NON GUI and GUI modes as well - tested in jMeter UI and in docker.

  1. In users variables, I added

JMETER_SCRIPTS_DIRECTORY=${__BeanShell(import org.apache.jmeter.services.FileServer; FileServer.getFileServer().getBaseDir();)}

user variable

  1. Before groovy file name, I put ${JMETER_SCRIPTS_DIRECTORY}

for example, JMETER_SCRIPTS_DIRECTORY/ScriptName.groovy.

assert cities groovy script in a separate file

0
votes
  1. You can just remove ./ from your script path so it would be just groovy/CitiesAssertion.groovy, this way the script location will be relative to the JMeter's base directory

    enter image description here

  2. If you want to proceed with your scripting approach consider migrating to __groovy() function

  3. There is no AssertionResult shorthand in JSR223 PostProcessor, you might want to switch to JSR223 Assertion