Jmeter assert json response element to be NOT NULL - jmeter

I am getting Json response, I have parsed it using jp#gc - JSON Path Extractor and got an element say 'Access_Token'. This Access_Token is dynamic. So I just want to make sure that this element is not null.
Any leads would be much appreciated.

In the JSON Path Extractor provide Default Value, for example NOT_FOUND
Add Response Assertion after the JSON Path Extractor and configure it as follows:
Apply To: JMeter Variable -> Access_Token
Pattern Matching Rules:
Tick NOT
Tick Equals
Patterns to Test: NOT_FOUND (or whatever you entered into the "Default Value" input of the JSON Path Extractor)
See How to Use JMeter Assertions in Three Easy Steps article for comprehensive information on using Assertions in JMeter scripts.

Add a BeanShell PostProcessor component after you get your Access_Token and in it check what you want...
if (vars.get("Access_Token") != null) {
// do something
} else {
// do something else
}
Depending on your needs, you can do basically what ever you want from here. For example stop the thread, stop the test...

Since JMeter 3.0 there is a new JSON Path Processor that you should use instead of the JMeter Plugins one.
See its features in action here:
http://www.ubik-ingenierie.com/blog/easy-scripting-of-json-applications-with-apache-jmeter/
You can then apply Dmitri T. answer.

In jmeter 5 you could try doing something like this:

Related

How to add a variable from Json extractor into an array in JMeter?

I'm new here and also a beginner on JMeter and maybe this was already answered in an old post that I didn't find, sorry if this is the case.
I had this Post request I need to send with all these IDs that vary according to the account
Post Request
In order to get all of the IDs, I used the JSon extractor to put then into a variable
JSon extractor, then I got all the FieldIDs that I need.
ID extracted
But now how can I add this variable inside the request? I tried something like {"ids":"${fieldId}","includeBoundary":true} but it didn't work. How can I use this?
Please see: HTTP Request parameter dialog example
If you need to extract the whole response, save it into a JMeter Variable and send it back to another endpoint - the easiest way is using Boundary Extractor providing empty left and right boundaries
If you need more complex transformations - take a look at JSR223 Test Elements and Groovy language
I solved my problem in a so easy way(damn it)!!!!
On the Json extractor I just marked the option "Computer concatenation var (suffix_ALL)" then on the debbuger I got all IDs I needed in only one line and finally on my request I just add on the body data the line {"ids": [${fieldId_ALL}],"includeBoundary":true} and bingo it worked like a charm!!!!

jmeter prev.getResponseDataAsString getting wrong return

I have a looping process that will extract some values from a web service (working) and loop through to pull all the information for each value (working).
I need to capture the whole return into a variable so I can modify it and post it back up later.
Screenshot:
When the "Baseline for ..." get kicks in, I get the proper response
But the "Get response" BeanShell PreProcessor is picking up old responses
Screenshot:
Given where my "Get response" object is, I would assume the:
vars.put("ResponceData", prev.getResponseDataAsString());
...would grab the response from "Baseline for ${ID} of site ${callSite}". Please help!
You are using wrong test element. Beanshell PreProcessor is being executed before request therefore it acts properly and returns response from the previous request instead of current one. You need to change it to the Beanshell PostProcessor and your code will start working as you expect.
It is recommended to avoid scripting where possible, if you need to save response data into a JMeter Variable you can do it using i.e. Regular Expression Extractor. According How to Extract Data From Files With JMeter article the relevant configuration will be something like:
Reference Name: ResponceData
Regular Expression: (?s)(^.*)
Template: $1$
If you face a JMeter limitation which cannot be worked around without using scripting make sure you are using the most performing scripting language, since JMeter 3.1 it is recommended to use JSR223 Test Elements and Groovy language

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