Regular expression default value - regex

Jmeter passes default value when trying to fetch a part of response in my case is a unique name which I want to pass in next request but it takes default value:
{"status":"success","body":{"uniqueName":"jpr1444130495897","voucherType":"sales","transactions":[{"creditAccount":{"uniqueName":"acctomerge","name":"Mayank"},"debitAccount":{"uniqueName":"dacc","name":"dummyaccount"},"amount":2228}],"voucherNo":1912,"date":"01-09-2015","tag":"dummydata","description":"Q3goK8QrUo"}}
In above response I have to capture uniquename & pass it in my next request. But by default it take default value.
Bean shell assertion contains:
${__setProperty(uniqueName,${uniqueName})}
Next request is this.
Still fetching default value. Somebody please help.

As Biffen said it is better to use JSON Path Extractor that comes with jmeter plugins (http://jmeter-plugins.org/wiki/JSONPathExtractor/). It's a lot easier for JSON - just put JSON path (probably in your case just uniqueName will be enough, but you can put there $.body.uniqueName) add a variable name as uniqueName as well, so there will be no problem to use it later as ${uniqueName} in the next request. And that's it.
Oh, and your post suggests that you try to extract it with Beanshell assertion instead of Regular Expression Extractor by the way - that also can be a case.

What exact uniqueName you need to extract? There are 3 of them in your JSON data:
jpr1444130495897
acctomerge
dacc
It isn't necessary to convert variable to property to use in next request. If both requests live in the same thread group - it's enough to use reference name like ${uniqueName}
For JSON data it's more handy to use JSON Path Extractor (available via JMeter Plugins project). See Using the XPath Extractor in JMeter guide (scroll down to "Parsing JSON") for plugin installation and usage instructions.

Related

URL encode Postman variable?

I use Postman for REST API testing and parametrize tests with global variables.
I should put a phone number into GET request: /path/get?phone={{phone}} but leading + sign in the phone number is interpreted as a space.
What is the syntax to URL encode global variables in Postman? Is it possible to run JS encodeURIComponent() on variable in URL?
I am late but still worth it:
Just highlight and right click the part of url you want to encode. Select encodeURIComponent
That's it.
Use the Pre-request scripts (it's next to body) for this:
var encoded = encodeURIComponent({{phone number}});
or
var encoded = encodeURIComponent(pm.environment.get("phone number"));
and to proceed, use:
pm.environment.set("encoded phone number", encoded);
And set your URL to /path/get?phone={{encoded phone number}}
Just a shortcut to Mohhamad Hasham' answer.
You can encode and decode direct in the Params Value field:
The trick is to get your environment variable in the pre-request script and then set it after encoding it
var encoded = encodeURIComponent(pm.environment.get("phone"));
pm.environment.set("encoded phone number", encoded);
This will work as well:
var encoded = encodeURIComponent(pm.request.url.query.get("phone"));
pm.request.url.query.remove("phone");
pm.request.url.query.insert("phone", encoded);
I came across this question looking for an answer to a similar question. For me, the variable was a JSON object. The endpoint I needed to hit was expecting an object list as a query parameter and I have no way to change that to be the request body.
As much as some of the answers helped, I ended up coming up with a combined solution. Also, some of the code given in other answers is outdated as Postman has updated their API over the years, so this uses methods that work on 7.22.1.
pm.environment.set("basicJSON", '[{"key1":"value1","key2":"value2"},{"key1":"value1","key2":"value2"}]')
var encoded = encodedURIComponent(pm.environment.get("basicJSON"))
pm.environment.set("encodedJSON", encoded)
This solution requires that both basicJSON and encodedJSON exist as environment variables. But what was important for me was the ease of editing the object. I didn't want to have to decode/encode constantly to change values, and I didn't want to have to open the environment variables dialogue. Also, it's important to note the single-quotes around the object. Excluding them or using double-quotes would cause Postman to send something like "[object Object]" which is useless to an endpoint expecting actual JSON.
I had similar problem with braces { and } in query parameter.
By turning off the following setting it started working for me.
For the postman version 9.28.4 ==>
You can use 2 methods:
By selecting the part of the url in url bar -> right click -> EncodeURLComponent. (screenshot attached)
You can also use "pre-request script" tab of postman and write the script for the variable manually. (screenshot attached)
The problem with right-click => Encode URI Component is that it destroys the raw value of that parameter. You can use the following pre-request script to overcome this (which also works for cases where you have disabled that param):
// queryParam is of type https://www.postmanlabs.com/postman-collection/QueryParam.html
if ((queryParam = pm.request.url.query.one("name_of_your_query_param")) !== undefined
&& queryParam.disabled !== true) {
queryParam.value = encodeURIComponent(queryParam.value);
}
Click the Params button to open the data editor for URL parameters. When you add key-value pairs, Postman combines everything in the query string above. If your URL already has parameters - for example, if you are pasting a URL from some other source. Postman splits the URL into pairs automatically.
https://www.getpostman.com/docs/v6/postman/sending_api_requests/requests
POSTMAN's documentation on building requests in the section "sending parameters" is helpful here. You can encode path data by simply encoding the URL with a colon, listing the key name of the encoded element, and then a new section will appear below the query parameters allowing you to customize values and add a description, just as we do with query params. Here's an example of encoding the URL for a GET request:
https://somesite-api-endpoint/:record/get
And here's what the display looks like after you add path data. Any value you add in the path variables section will automagically update the URL with your data.

Viewstate Replacing error. [ViewStateException: Invalid viewstate. ]

Tried with JMeter: How to know why my regular expression extractor in JMeter is not extracting the data but still not able to replace my view-state, its throwing [ViewStateException: Invalid viewstate. ] error . Please check the attachment , an my script has not _EVENTVALIDATION .
enter image description hereViewstate
viwstate2
As i am observing from your attached image
You need to pass same `Reference name` as a value of __VIEWSTATE in Next request
First Cross verify, Is your correaltion is correct or not? (you can use debug sampler to check the correlation)
If yes, then first mistake is, you had given reference name as "VIEWSTATE" in below shown snapshot
But you are passing ${jsfViewState} in other snapshot, so correct it and pass same reference name i.e. ${VIEWSTATE}
It'll resolve your issue, if issue still persists then click on encode button beside Name-Value pair because sometimes there is some mismatch in encoding format between the response which we capture and the value which we have to pass
Don't be confused with ASP.NET ViewState and JSF Viewstate, they have similar nature but different underlying technologies and different parameter names
Given you correctly correlated the value, I believe you need to change __VIEWSTATE parameter name to javax.faces.ViewState and it should work.
See Testing a JSF Application with JMeter guide for a little bit more detailed explanation on the topic and How to debug your Apache JMeter script guide for some troubleshooting techniques.

How to decode URL with BeanShell?

I need to pass a parameter (part of URL) in a request for example:
"S;4;163;1;O;rAghjgjU="
But this value is extracted as a regular expression like that:
"S%3B4%3B163%3B1%3BO%3BrAghjgjU%3D"
I want to use BeanShell to decode the URL.
JMeter provides __urldecode() function so if you use it like:
${__urldecode(S%3B4%3B163%3B1%3BO%3BrAghjgjU%3D)}
you'll get the required value.
If you still want to go for Beanshell, you can invoke URLDecoder.decode(); method like:
String decoded = URLDecoder.decode("S%3B4%3B163%3B1%3BO%3BrAghjgjU%3D", "UTF-8");
There is also __Beanshell() function which allows executing arbitrary Beanshell script, invoke it like:
${__BeanShell(URLDecoder.decode("S%3B4%3B163%3B1%3BO%3BrAghjgjU%3D"\, "UTF-8"),)}
See How to Use BeanShell: JMeter's Favorite Built-in Component for more information on Beanshell scripting in JMeter, in particular pay attention to how Java and JMeter API classes are invoked from within Beanshell scripts.

Unable to call object from a List located in external BeanShell script [Jmeter]

My end goal here is to get a string from a list that is created dynamically from a JSON response. I am having troubles calling a value from the list that is created in an external BeanShell script. The external script reads the JSON response and creates the list from the filtered response.
import com.jayway.jsonpath.JsonPath;
import com.jayway.jsonpath.Filter;
try{
String json = new String(prev.getResponseDataAsString());
List allPositiveAffects = JsonPath.read(json, "$.affectTags[?(#.value > 0.0)].key", new Filter[]{});
vars.putObject("allPositiveAffects",allPositiveAffects);
log.info("allPositiveAffects: " + allPositiveAffects);
int positiveAffectCount = allPositiveAffects.size();
vars.put("positiveAffectCount",Integer.toString(positiveAffectCount));
} catch (Throwable ex){
log.error("Error:\n", ex);
}
allPositiveAffects returns the expected values, so now I want to get one of those values into a subsequent JSON POST.
My Jmeter test setup:
- Thread Group
+ JSON GET request
- BeanShell PostProcessor
+ JSON POST request
Attempting to get a value from allPositiveAffects has proven to be not as easy as calling allPositiveAffects.get(n) like I can within my BeanShell script.
I haven't figured out an easy way to get the contents of the Lists. Am I missing something? I have tried several different things in my JSON POST request. The latest being:
//json POST data
{
"entries":[
"id": -1,
"key": "${__BeanShell(${allPositiveAffects}.get(1))}"]
}
Which returns the following error:
java.lang.ClassCastException: net.minidev.json.JSONArray cannot be cast to java.lang.String
Any help on a solution or workaround for this would be greatly appreciated. It would be a lot easier for me to be able to call a List instead of creating various variables.
I believe that you could just add .toString() to your expression like:
`${__BeanShell(${allPositiveAffects}.get(1).toString())}`
By the way, Beanshell has some performance overhead so I would rather suggest going for JSON Path Extractor which is available via Extras with Libs Set of JMeter Plugins package, it should be more convenient and less resource consuming.
See Using the XPath Extractor in JMeter (scroll down to "Parsing JSON") for installation instructions and some JSON Path language reference examples.

Regular Expression Extractor not maintaining the variable value

Regex Extractor is working fine at first. I can get it to pull the correct value - a groupID - from the response in the previous HTTP request. And, I turn around and use the variable in the HTTP request immediately after the Extractor. That works great as well. I can see the value in the response was the same as the one placed in the post for the next request.
Few requests later I try to use the variable again when I need the groupID passed again. But, when I look I see the default value I put in the Extractor instead of the value from the response.
My question is two fold: is this extractor only expected to generate a temporary variable?
And, if so, is there a way to plug this variable into something else for later reuse?
Or, did I just miss something?
Well, I found my mistake. It would have helped if I read the information under the post-processor section before I read about the extractor...
I did not have the extractor as a child of the HTTP request. I had it as the next step.
Once I nested it as a child all was right with the world. The variable stays as it was and is not changing after every step.
Lesson learned - RTFM. Or, at least the online documentation a bit more thoroughly!
Jamie