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

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 <

Related

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 - Convert Unicode Hex Char to "normal string"

I automating a script in which I am using a regular expression to extract the path of the next HTTP request, the problem is that the value matched with the regular expression contains things like /, so I need to convert that into/, in order to be able to send the path in a valid format.
I expect that it should be possible to do that using a PostProcessor, but I am not sure of how it should be scripted.
Thanks in advance.
If they are the proper HTML/XML-encoded characters, you can use inline function __unescapeHtml, e.g.:
${__unescapeHtml(/)}
The function above uses Apache's StringEscapeUtils under the hood, so you can call it from BeanShell post-processor:
import org.apache.commons.lang3.StringEscapeUtils;
vars.put("myvar", StringEscapeUtils.unescapeHtml4("/"));
The same library has a bunch of other functions, that may be useful, depending on what you are decoding from, for example:
import org.apache.commons.lang3.StringEscapeUtils;
vars.put("myvarxml", StringEscapeUtils.unescapeXml("/"));

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

Extracting javascript variable and passing to next Jmeter request

The Response data look like following within Script tag
var csrfParam =
"SKtEMgZtO0H8EYHkAZIQ4tcS5bC8jKrD=y2G5Of06jgHbkNdHNleFKiXoBMC62veD";
var securityTokenName = "SKtEMgZtO0H8EYHkAZIQ4tcS5bC8jKrD";
var securityTokenValue = "y2G5Of06jgHbkNdHNleFKiXoBMC62veD";
I'm using regular expression extractor as following:
Reference name : MYREF
Regular Expression : securityTokenName ="(.+?)" securityTokenValue="(.+?)"
Template : $1$$2$
I'm access this variable in next Jmeter request to build the URL:
${MYREF_g1}
${MYREF_g2}
Expecting to get MYREF_g1 =SKtEMgZtO0H8EYHkAZIQ4tcS5bC8jKrD
and MYREF_g2 =y2G5Of06jgHbkNdHNleFKiXoBMC62veD
But THIS is not working .
Any help would appreciated!
It looks like that you're misunderstanding what groups and templates are.
As per Using RegEx (Regular Expression Extractor) with JMeter
Template. The template used to create a string from the matches found.
This is an arbitrary string with special elements to refer to groups
within the regular expression. The syntax to refer to a group is:
'$1$' to refer to group 1, '$2$' to refer to group 2, etc. $0$ refers
to whatever the entire expression matches. So, if you have in response
word “economics” and search for regular expression “(ec)(onomics)” and
apply template $2$$1$ than in output variable you will receive
“onomicsec”.
So your RegEx should look like:
var securityTokenName = "(.+?)"; var securityTokenValue = "(.+?)";
So
securityTokenName will be stored in JMeter Variable MYREF_g1
securityTokenValue will be stored in JMeter Variable MYREF_g2
View Results Tree Listener has built-in RegExp Tester on ResponseData tab. You can also use Debug Sampler to see what variables have been set by your Regular Expression Extractor.
You need multiline regex match. Your regex will look like this
(?s)securityTokenName="(.+?)".*?securityTokenValue="(.+?)";
A sample test plan (it uses dummy sampler from jmeter plugins, if you don't have them it will faile) is here.
This post here discusses that simply having .*? will match multiple line, but apparently it did not work. This post wast the savior.
Details are from the jmeter documentation are here.
Please note that I have removed the spaces around = to simplify my sample. Please change the regex appropriately.

help in storing a value and calling a variable in jmeter request using regular extractor

my soap/xml response looks like below:
<Account><Accountnumber>1234<Description>savings</Account><Account><Accountnumber>1235<Description>Savings1</Account>
I would like to store accountnumbers in a variable or array and would call it in another soap xml request in jmeter for knowing their details. can somebody help me how i can store and how i can call that variable ? I am new to Jmeter.
Thanks in advance.
If the account numbers are static, you're better off using a .csv file, as mentioned by Vance because the CSV data reader has less overhead then regex.
However, if you want dynamic data, it's very easy to do.
Download "regex coach" to help you write regular expressions. It's an amazing tool.
Attach a "regular expression extractor" as a child to your SOAP/XML request
Run the request once, to get the reponse
Copy the response into regex coach (or whatever tool you use), and write your regex. It'll look something like this: (\d+?)\D (look for any digit after the text accountNumber and stop after a non-digit)
Configure the rest of the regex. In this case, you'll want:
Apply to: Main Sample Only
Response filed to check: Main Body
Reference Name: VariableName
Regular Expression: See step 3
Match No: 1 (1st match) 0 (any match) or -1 (all
matches, useful when doing "FOR EACH
found" logic
Default Value: failed
TO use your variable account number in other requests, simply use the reference name. In this example: ${VariableName}
Reference: http://jmeter.apache.org/usermanual/component_reference.html#Regular_Expression_Extractor
You may save your data in a ".csv" file and Jmeter can read it easily through its csv data set config.
Use ${your data variable} in your scripts.

Resources