Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to parametrize JMeter Loop Controller with Groovy function?

I am pretty new to Jmeter but can handle "simple" test plans. I really do need help with the issue I am facing. Currently I run into a problem regarding how to parametrize the Loop Controller loop count Groovy function.

I did found a lot of examples but they are all with static loop count like: ${__groovy(new File(‘test.csv’).readLines().size(),)}.
I really would like to parametrize the part ‘test.csv’. Therefor I replaced that with ${csvFile}, like:


[enter image description here][1]
${__groovy(new File(${csvFile}).readLines().size(),)}.

In the ‘User Defined Variables’ section of the ‘Test Plan’ I created the variable ‘csvFile’.
Test plan image in here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

When I execute the script I retrieve an error in the log like:

2022-01-25 11:35:14,870 WARN o.a.j.f.Groovy: Error running groovy script
javax.script.ScriptException: org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
Script16.groovy: 1: Unexpected input: '\' @ line 1, column 12.
   new File(D:\devenv\projecten\JM_InterestDeterminationService\inputmessages.csv).readLines().size()
              ^

1 error

Is the error pointing to the backslash? Is that because of the Windows OS?

Must I replace the ‘\’ with a ‘/’? How and where can I do that?
I started fiddling around with .replaceAll("\\", "/") and .replaceAll("\\\\", "/"), but I don’t know where to put it and what the correct syntax is.

Could any of you please help me?

>Solution :

That’s one of the reasons why it’s not recommended to inline JMeter Functions or Variables into Groovy scripts.

Just use vars shorthand for JMeterVariables class instance to read your csvFile variable value like:

${__groovy(new File(vars.get('csvFile')).readLines().size(),)}

and it should resolve your issue.

More information on this and other JMeter API shorthands: Top 8 JMeter Java Classes You Should Be Using with Groovy

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading