JSR223 script to point to file location throws error - jmeter

i have added JSR223 pre-processor to point to the file that needs to be picked by the http request. when i run the request in a loop, it needs to pick different file each time so, the file location is bit complicated. So, i tried something like below:
filePath in JSR223
But, this throws an error. should i be using two back slash? that throws error too.
I am getting the folder in the project location by using:
${__BeanShell(import org.apache.jmeter.services.FileServer; FileServer.getFileServer().getBaseDir();)}\FileToUpload\
But, i need to go to different folders on each thread hence the complicated way to get the file through JSR223 script. Is there a easier way?

First of all starting from JMeter 3.1 you're supposed to be using JSR223 Test Elements and Groovy language so consider migrating to __groovy() function
Second you need to use triple slashes because you need to:
escape each backslash in Java strings with another backslash
third backslash is to handle JMeter Functions escape meta character (for example if your function contains comma you need to escape it with the backslash like \,
${__groovy(org.apache.jmeter.services.FileServer.getFileServer().getBaseDir() + '\\\Test Plan.jmx',)}
Alternatively you can use forward slashes:
${__groovy(org.apache.jmeter.services.FileServer.getFileServer().getBaseDir() + '/Test Plan.jmx',)}
or go for file.separator property
${__groovy(org.apache.jmeter.services.FileServer.getFileServer().getBaseDir() + System.getProperty('file.separator') + 'Test Plan.jmx',)}

Related

How to define special characters in JMeter parameterization

I am trying to parameterize my request using CSV Data Set Config. My input includes double quotes("), colon(:) and brackets([])
Eg: fiscal_year ":["2021",2019]"
Had tried with it, but in the actual results its passing as "fiscal_year "":[""2021""
Please share your inputs on what am i missing on the input paramter.
I don't think it's due to double quotes("), colon(:) and brackets([]), CSV stands for comma-separated values so JMeter treats it as a delimiter and reads everything including to the first comma.
So you might want to change the "Delimiter" to something else:
It's hard to come up with a comprehensive solution without seeing at least couple of lines from your CSV file and the way you're parameterizing the HTTP Request with JMeter Variables
In case you have one entry per line in the CSV file it might be easier to go for __StringFromFile() function which reads next line from the file each time it's being called. See Apache JMeter Functions - An Introduction for more information.

Jmeter beanshell Encounter ";"

I am trying to execute beanshell script in jmeter for a URL parameter value. I have the following:
${__BeanShell(vars.get("query").replaceAll(" ","%20"))}
The jmeter console outputs this:
Caused by: bsh.ParseException: In file: inline evaluation of: ``vars.get("query").replaceAll(" ";'' Encountered ";" at line 1, column 33.
I can't figure out what the problem is as the character there is a , not a ;.
You're doing something ridiculous. Encoding an URL is not only about escaping spaces, looking into URLEncoder documentation you will need to handle:
All non-Latin non-alphanumeric characters
All characters apart from ., -, *, and _
Which might be very tricky.
So you basically have 2 options:
Use JavaScript encodeURIComponent() function via JMeter's __javaScript function like:
${__javaScript(encodeURIComponent("${query}"),)}
Or use aforementioned URLEncoder from __groovy() function like:
${__groovy(URLEncoder.encode(vars.get('query')\, 'UTF-8').replaceAll('\\\+'\,'%20'),)}
Performance-wise in cases of high loads Groovy is preferred option, check out Apache Groovy - Why and How You Should Use It article for more details.
See JMetrer's functions tutorial, you need to escape every comma:
If a function parameter contains a comma, then be sure to escape this with "\", otherwise JMeter will treat it as a parameter delimiter.
In your case
${__BeanShell(vars.get("query").replaceAll(" "\,"%20"))}
Also consider using __groovy function instead of __BeanShell for better performance.
Please use below code in Beanshell PreProcessor or BeanShell PostProcessor in order to replace single space character to '%20':
String myString = vars.get("query");
String new_var = myString.replaceAll(" ", "%20");
vars.put("updated_value", new_var);
You can further use 'updated_value' variable having space replaced by '%20' in next requests.
Please refer to the JMeter Knowledge Base for more information on JMeter elements.

JMeter is adding additional character to auth token

I am using JMeter 3.1.
I am sending the following path
core/DocumentRenderer/api/cmbs/54ad43a8cbda7177fc83b2c5?Authorization=${authentication}
I am receiving back
core/DocumentRenderer/api/cmbs/54ad43a8cbda7177fc83b2c5?Authorization=%20A3BB6A2F90D13D76E99483509DE3F864BB51C8424ACA5792D651F43C2B20A65C42ACEAB0DC03493060EF59E2654C482AC38333AB4BFBAAB2E073809CE8E97E4C7E62B02D766ABF3344818D13A981D40C447AFC3458AAE54E92705D20D0C49D7A8565B72C227A2663C3944931F4B747840E8FE01B599A2A5C205E39973A8A98DDD80BE61E46C8E3222334CF4C1A512FADED5EF5C9A493333058BE70A04DA835FA045AF474BFDFBD84878F53182E3B6726
with %20 as an additional character.
I also tried to add authorization as parameter
Authorization=${authentication}
but then it adds or + if I use Encoded=Yes or space if Encoded unchecked.
Any suggestions?
JMeter doesn't add anything, most probably your Post-Processor which extracts the token value is obtaining an extra space so your ${authentication} variable basically has a space in the beginning, you can double check it using Debug Sampler and View Results Tree listener combination. Amend your Post-Processor logic so it won't include the space into the variable.
Another option is using i.e. __javaScript() function to remove spaces from the {authentication} variable itself like:
core/DocumentRenderer/api/cmbs/54ad43a8cbda7177fc83b2c5?Authorization=${__javaScript("${authentication}".replace(/ /g\,''),)}
Demo:

Jmeter- How to pass Comma separated String as 1 value through parametrization

From a csv file, I need to pass
224,329,429
as a single value to one of the parameter in HTTP request.
I have parameterized using CSV data config. But, only 224 is getting passed.
I want 224,329,429 to be treated as a single value.
Please let me know how do I achieve this. Should I change anything in CSV config or CSV file to make this work?
Just use __StringFromFile() function instead of using CSV Data Set Config.
The __StringFromFile() function reads next line from the file each time it's being called so it seems to be a lot easier to stick to it for particular your scenario.
The syntax is as simple as ${__StringFromFile(/path/to/your/file.csv,,,)} and the function can be used anywhere in the script, i.e. directly in the request parameter section.
See Apache JMeter Functions - An Introduction to get started with the JMeter Functions concept and comprehensive information on the above and other JMeter functions.
You should change your delimiter to a not used character e.g. #
In that way you will be able to get full line for every request
Use ${__FileToString(dummy.csv,,payloadvar)} function. It makes the file independent that mean you can use any file extension example: .txt, .csv, .excel etc..
Just keep the string in dummy.csv and it will fetch the whole string.
benefit of using this function is, it will not consider comma's so in case your string has comma separated values then this is the best option.
Just use %2C in the place of comma.

Jmeter - How Can I Replace a string and resend it?

I'm trying to create a script that will take a URL out of a response and send it out again.
Using the regular expression extractor I've succeeded in taking the wanted URL, but it holds "&" so naturally when sending it out the request fails.
Example:
GET http://[ia-test01.inner-active.mobi:8080/simpleM2M/ClientUpdateStatus?cn=WS2006&v=2_1_0-iOS-2_0_3_7&ci=99999&s=3852719769860497476&cip=113-170-93-111&po=642&re=1&lt=0&cc=VN&acp=&pcp=]/
I'm trying to replace the "&" with a "&".
I've tried: ${__javaScript(${url}.replace("&","&"))}
But it did not work. I've tried the regex function as well- the same.
I'm not sure the IP field in the request supports the us e of functions.
I'm currently trying to use the beanshell post-processor. But I'm pretty sure there is a simpler solution I'm missing.
Not sure what you're trying to get by replacing & with & however will try to respond.
First of all: given multiple & instances you need to use replaceall function, not replace
Second: replace / replaceall functions take a RegEx as parameter, so you'll need to escape your &
If you're trying to substitute URL Path in realtime, you'll need Beanshell Pre Processor, not the Post Processor
Sample Beanshell Pre-Processor code
import java.net.URL;
URL myURL = sampler.getUrl();
String path = myURL.getPath();
String path_replaced = path.replaceAll("\\&", "&");
vars.put("NEW_PATH", path_replaced);
After that put ${NEW_PATH} to "Path:" section of your HTTP Request.
Hope this helps.
Solution with less code:
Install the Custom JMeter Functions plugin
Use the following syntax
${__strReplace(ImAGoodBoy,Good,Bad,replaceVar)}
‘ImAGoodBoy’ is a string in which replacement will take place
‘Good’ is a substring to be replaced
‘Bad’ is the replacement string
‘replaceVar’ is a variable to save result string
Refer this URL for more info!
Thank a lot. However, i see from a recent experience that to replace a character that is actually a RegExp special character, like \ " ( ) etc, you need to put 3 backslashes and not 1, not 2. This is weird.
so you write
var res = str.replaceAll("\\\\u003c", "<");
to replace \u003c with <

Resources