How to store entire encrypted response, unencrypt it with POST method and update the response for next rest call using Jmeter - jmeter

Run Get method to get the data of the policy - response is encrypted.
Run a Post method to decrypt the response from step 1
On the response from Step 2 there is the field policyStatus and will be changed to value = 2
Encrypt the decrypted payload with changes in the policyStatus. I created a regular expression extractor in step 2 to get the response, but the problem is if I directly parameterized it in this step I wont be able to change the value of policyStatus, is there a way to change the value after extracting without posting it so that I can encrypt it and then post it.
I tried the __strReplace() function but this will be possible if the request is step 4 is not parameterized. see below
Actual Body(Parameterized):
{
"isEncrypt": true,
"payload": "{${payload}}"
}
Body(Not Parameterized):
{
"isEncrypt": true,
"payload": "{\"policyNumber\":\"\",\"policyStatus\":\"1\",\"ownerName\":\"\",\"exchangeRate\":1,\"product\":{\"productName\":\"",\"category\":\"T\",\"shariaIndicator\":\"",\"currency\":\""},\"billings\":[{\"type\":\"",\"amount\":0,\"currency\":\""},{\"type\":\"",\"paidToDate\":\"2019-07-08\",\"mode\":\"",\"amount\":,\"currency\":\"",\"minimumPayment\":,\"outstandingPeriod\":0,\"paymentSuspend\":}]}"
}
the policyStatus is inside the payload field which comes from the response on step 2.
tried the advice from this --> How to store entire response and update it for next rest call using Jmeter
but this applies in the value of the single field.

Add JSR223 PostProcessor after the Regular Expression Extractor in step 2
Put the following code into "Script" area:
vars.put('changedPolicy', vars.get('myVar').replace("\\\"policyStatus\\\":\\\"1\\\"","\\\"policyStatus\\\":\\\"2\\\""))
Replace myVar with your actual JMeter Variable reference name from the Regular Expression Extractor
That's it, the JSR223 PostProcessor will generate changedPolicy variable with the policyStatus value set to 2
Demo:
vars keyword used in the Groovy script is the shorthand for JMeterVariables class instance, it is used for reading myVar variable value and writing the replacement into changedPolicy variable. See Top 8 JMeter Java Classes You Should Be Using with Groovy to learn more about JMeter API shorthands available for Groovy scripts.

Related

In JMeter when I try to pass the variable assigned with JSON extractor to the subsequent request null value is passed

I do a POST request using Jmeter and I parse the JSON response using the JSON extractor. When i use the debug sampler I could see the Variable is assigned with the value properly but that value goes as null in the subsequent request
Below is the request URL for the next sample /XXX/XXX/XXX/XX/${recordingjobid}
but this value recordingjobid is not substituted with the value.
I can only think of not proper placement of the JSON Extractor.
For example if you have it at the same level as several samplers - it will be run after every of them (including the Debug Sampler) and each next one will be overwriting the value set by the previous one as JMeter Variables are local to the thread and don't allow duplicates.
So my expectation is that if you move the JSON Extractor to be a child of the request which returns the JSON - it should resolve your issue.
More information: JMeter Scoping Rules - The Ultimate Guide

How to loop an HTTP request and update the variables each time in Jmeter BeanShell

I have 2 HTTP request: One to GET data from an api and the other to POST data to the api.
The GET request brings several users in JSON. The POST request requires to 1 request per user. Hence I need to:
Loop the same POST request several times depending on the number of users (already did this by using a while controller that checks the number of users from the JSON response).
For each POST request, I need the variables used in such request to be updated depending on the user's information inside the JSON response.
The approach I am attempting is to use BeanShell PreProcessor inside the POST request but I'm having troubles with it.
Suppose one variable is called ${name} in the request body of the POST. I am using a JSON Extractor PostProcessor (On the GET request) path: "Travelers[0].FirstName" that returns the first name of the first user but then I need the second user's name (Travelers1.FirstName) to be assigned to the same variable ${name} before the POST request is sent and so on with every single user.
I want to make a for loop like this:
for (int i = 0; i <= numberOfTravelers; i++) {
vars.put("Name", Travelers[i].FirstName)
}
The problem is that I do not know how to call a JSON path from a JSON response of another previous request. Is there a way to reference the jsonpath to the specific JSON response or a way to save the whole JSON response in a variable and then find the specific value inside that variable as a JSON path.
I have tried this with the JSON extractor but the problem is that if I use the path: Travelers[*].FirstName, it will actually get all the names on the JSON but the variable ${name} will only store ONE name, not all of them as an array that I can later access in my for loop with a normal variable ${name[i]}. That is why I need to access the JSON path from the BeanSheall.
Here a sample of the JSON response:
{
"Travelers":
[
{
"FirstName":"VICTOR",
"Surname":"ORREGO",
"Key":"1.1",
"PassengerRPH":1,
"TypeCode":"ADT"
},
{
"FirstName":"JULIO",
"Surname":"OZAETA",
"Key":"2.2",
"PassengerRPH":2,
"TypeCode":"ADT"
}
]
}
This is the PostProcesor JSON Extractor at the GET request that I'm using. it is currently assigning the first name it gets from the JSON response (Victor) to the variable ${Name}
I need that in the next iteration (of the POST Request) the variable ${Name} to return the next name in that path, which is Julio.
here is the solution..
Add a JSON Extractor to the get request .. use match no -1 to store all Firstnames as show below.
i'm extracting all Firstnames and storing it in JMeter variables with a single JSON extractor
2. To the same get request add a JSR223 Post processor and set the counter value to 1
vars.put("counter","1");
Add a while loop to the test plan and add the following condition to the while loop.
${__javaScript(parseInt(${counter})<=parseInt(vars.get("FirstName_matchNr")),)}
4.To the post request add a JSR223 Pre Processor and add the following code
vars.put("name",vars.get("FirstName_"+vars.get("counter")));
This will store FirstName_Matchno's value in name variable.
Add a JSR223 Post Processor to the POST Request and increment the counter.
int counter = Integer.parseInt(vars.get("counter")) +1;
vars.put("counter",Integer.toString(counter));
You can see in the results its substituting a different name on each iteration of loop
Let me know if it helps..

Extracting value from jmeter post request

I want to extract value of the parameter sent through post request in jmeter.
I know the use of regular expression for extracting response value or request URL but here I would like to extract the value of post request.
I've been thorough how to extract value from request in Jmeter but it didn't worked.
Not sure why do you need it as given you sending "something" you should already have that "something" hence you don't need to extract it, however here you go:
In order to save 1st parameter value (or the whole post data if you use "Body Data" mode):
Add Beanshell PostProcessor as a child of the HTTP Request.
Put the following code into the PostProcessor's "Script" area:
String request = ctx.getCurrentSampler().getArguments().getArgument(0).getValue();
vars.put("request", request);
You will be able to access extracted value as ${request} where required.
Clarifications:
ctx - shorthand for JMeterContext class instance
getCurrentSampler() - in case of HTTP Request sampler stands for HTTPSamplerProxy
See How to Use BeanShell: JMeter's Favorite Built-in Component guide for more information on using JMeter and Java API from Beanshell test elements in your JMeter test.
I added a Beanshell PostProcessor in my http request with following code.
import org.apache.jmeter.config.Argument;
import org.apache.jmeter.config.Arguments;
Arguments argz = ctx.getCurrentSampler().getArguments();
for (int i = 0; i < argz.getArgumentCount(); i++) {
Argument arg = argz.getArgument(i);
String a = arg.getValue();
vars.put("EMAIL",a);
}
Explanation: I get a my request as a json and put it in EMAIL. Now I can use EMAIL as a variable in my other request.
Then, I added a jp#gc Json Path Extractor and I applied it to a Jmeter Varaible.
Now, Email will be used as variable, which contains my json request and I can extract using jsonPath Extractor.
An easy way to do this is using the JSON Path Extractor.
There are just
For the example you gave
{ "data" : { "name" : "john_doe", } }
'Variable Name: YourNewVar'
'JSON Path: $.data.name'
Should work, but you may need to do some experimenting.
You may want to add a "debug sampler" (its one of the standard samplers) and put in its title $YourNewVar so you can see what is being extracted.
Beanshell and "Regular Expression Extractor" will work, of course, but may be a little harder to use if you are not familiar with them.

Manipulating the request body of HTTP thread based on the data extracted from the previous HTTP response

I want to manipulate the request body of HTTP thread based on the data extracted (using 'Regular Expression Extractor') from the previous HTTP response.
Here is the scenario:-
I have extracted the statusFlag and statusId from 'HTTP request 1' as:
Ref name: status
Reg. Exp: "statusFlag":"(\w+)","statusId":"(\w+)"
So, first I want to check that the value of statusFlag is 'New' or not.
If it is New then I have to proceed and feed statusId in next HTTP request or else display statusFlag mismatch.
Need help. Got stuck badly.
I believe Response Assertion is what you're looking for. Add it after the Regular Expression Extractor and configure it as follows:
Apply to: JMeter Variable -> statusFlag (or your reference name)
Pattern Matching Rules: Equals
Add New as a "Pattern to Test"
The assertion will check whether "statusFlag" is "New" and if not - it will fail the sampler and report the expected and the actual value.
Optionally you can add If Controller after the Response Assertion, use ${JMeterThread.last_sample_ok} as a condition and place 2nd request as a child of the If Controller - it will be executed only if "statusFlag" is new.
See How to Use JMeter Assertions in Three Easy Steps guide for more information on conditionally setting pass or fail criteria to requests in your JMeter test.
That's how your Jmeter project should look like.
Regular Expression Extractor stores extracted value in ct variable that can be accessed in If Controller as "${ct}" == "yourvalue" and, if true, can be also sent as a part of Request 2 body using the same ${ct} reference.
Jmeter project structure

How to Get, Assign the Span Id value in JMeter Beanshell?

I have created a User Defined Variable with name as "Status" and a default value as "Started".
I got a HTML Response, with following content:
<SPAN id="ApplicationStatus"> Interrupted</SPAN>
I want to get the Span Id value and use in beanshell samplers to process further either in If Controller or Switch Controller.
I used Regular Expression extractor to extract the value needed and its working too.
But when i say vars.get("Status") will always return me the default value "Started".
Is there a way where i can extract the required value "Interrupted" and substitute that to the user defined variable "Status"?
Yes you can get that value of #ApplicationStatus into your User Defined Variable (UDV).
You can use regex but really you shouldn't for this type of parsing I'm not going to get into many reasons why.
Here is how you can do it using alternative (better solution IMHO) :
String html = "<SPAN id=\"ApplicationStatus\"> Interrupted</SPAN>";
Document doc = Jsoup.parse(html);
String value = doc.select("#ApplicationStatus").first().text();
//Put value in UDV Status
vars.put("Status", value);
You can add this to your sampler that does this kind of parsing i.e Beanshell sampler, here are the imports (which go above this code) :
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
Please note that this code is Jsoup dependent so you will need to download jsoup jar and put it in your $JMETER_HOME/lib directory.
Hope this sheds some light on your issue.
Update
If you want to avoid Java, I've written small jmeter post processor component that extracts text value from HTML element. Take a look at :
https://github.com/c0mrade/Html-Extractor
If you go over the steps how to install the post processor from the page above, you would use it as follows :
Right click on your sampler. Add a Post Processors -> Html Extractor , in the jquery selector field write #ApplicationStatus and store result in variable of your choice (Status). Following this add Debug Sampler, if in your Debug sampler there is variable Status with the value Html Extractor is working! you're done!
I can't reproduce your issue.
Here is my plan:
- User defined variables with variable Status
- Thread Group
- HTTP Request
- Regular expression extractor with reference name = Status
- Beanshell Sampler that logs Status variable
Beanshell sampler logs value that was received in Regular Expression Extractor

Resources