Jmeter beanshell Encounter ";" - jmeter

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.

Related

IF condition in Groovy

Please could someone help me with this simple IF condition in Groovy? I have obtained a value (],) from a RegEx and am trying to use it in the subsequent groovy IF condition. I had the below which is not working:
${__groovy(vars.get("Results_Flag").contains("],"),)}
You need to escape comma , with a backslash \ so your condition should look like:
${__groovy(vars.get("Results_Flag").contains("]\,"),)}
Take a look at JMeter Functions documentation:
If a function parameter contains a comma, then be sure to escape this with "", otherwise JMeter will treat it as a parameter delimiter.
If you take a look at jmeter.log file you should see something like:
invalid variables in node If Controller
org.apache.jmeter.functions.InvalidVariableException: __groovy called with wrong number of parameters. Actual: 3. Expected: >= 1 and <= 2
More information: 6 Tips for JMeter If Controller Usage

Skip special character \ from regular expression in Jmeter

From the string - code-challenge=ndh37hdjdhf\hdhjf-ybd_536\x26
I want to capture only - ndh37hdjdhfhdhjf-ybd_536
I tried regular expression code-challenge=(.+?)\\26, but it takes the character \ along with other, i dont want this character to be part of the regular expression. How i can achieve this.
Please help on this.
Replace this \ character in the resulting JMeter Variable using __strReplace() function like:
${__strReplace(${foo},\\\\,,)}
__strReplace() function can be installed as a part of Custom JMeter Functions bundle using JMeter Plugins Manager
If you cannot or not willing to use plugins the same can be achieved using JMeter's built-in __groovy() function like:
${__groovy(vars.get('foo').replace('\\\'\,''),)}
Demo:

JSR223 script to point to file location throws error

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',)}

Jmeter Regular expression extractor and parsing into request

In Jmeter, I can able to extract the value using regular expression extractor, but while parsing the value I need some changes in the value as below.
Example,
Suppose If I extract this value in regular expression extractor (single Value in multiple lines),
PHNhbW+xwO
U0FNTDoyL
cmFjbGUu+
Here
I Need to replace + by %2B, need to add %OD%OA at the end of each line and multiple lines to a single line as below.
PHNhbW%2BxwO%OD%OAU0FNTDoyL%OD%OAcmFjbGUu%2B%OD%OA
I need to parse this as a single parameter value.
I think you can use __javaScript() function which allows calling arbitrary JavaScript code and inside JMeter __javaScript() funciton you can call EcmaScript EncodeURIComponent() function
It will automatically convert all newline characters \r\n to %OD%OA and + to %2B and in fact any character except alphanumeric and these ones:
_ . ! ~ * ' ( )
Demo:
See Using JMeter Functions guide for comprehensive information on how to deal with the functions in JMeter

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