Jmeter - passing JSON response array to the next graphql http request - jmeter

I have 2 graphql http requests as following.
The first http request's response returning json like so:
{"data":{"customers":{"customerIds":["0e1c7b05-79c6-40c6-9144-7230a836fe04", "45677b05-79tt-56c6-9144-7230a836bbbb"]}}}
I then created the json extractor and the beanshell to save the customer ids in the property
The 2nd Http request then using the above customerids property as part of the graphql request body
but I;m getting the 500 InternalServerError due to the quotes in the list customerids below not being escaped. how can i escape them ?
POST data:
{"query":"query getCustomerInfo {\n getCustomerInfo(customerIds: \"["0e1c7b05-79c6-40c6-9144-7230a836fe04"]\") {\n firstName\n lastName\n school\n }\n}"}

Don't inline JMeter Functions or Variables into your scripts
When using this feature, ensure your script code does not use JMeter variables directly in script code as caching would only cache first replacement. Instead use script parameters.
Don't use Beanshell, since JMeter 3.1 you should be using JSR223 Test Elements and Groovy language for scripting
So replace your Beanshell Assertion with the JSR223 PostProcessor and use the following code:
props.put("customerIds",vars.get("customerIds").replaceAll("\"","\\\\\""));
This way you will get the following customerIds property value:
[\"0e1c7b05-79c6-40c6-9144-7230a836fe04\",\"45677b05-79tt-56c6-9144-7230a836bbbb\"]

Related

Jmeter - how to return value in jmeter to HTTPS request

I was using JSR223 preprocessor and passing value to it's main request but value is not being return in that request.
How do I pass the value in the HTTPS request extracting from JSR223 preprocessor. In more I need to do parametrization. I am calling NodeJS code.
You need to use vars shorthand for JMeterVariables class instance in order to save the generated encoded value into a JMeter Variable.
i.e. in the last line of your script do something like
vars.put('password', response)
and in the HTTP Request sampler's request body use something like:
"password":"${password}"
More information: Top 8 JMeter Java Classes You Should Be Using with Groovy

Unable to correlate value[token], from one sampler to another in JMeter

I have recorded script using BlazeMeter plugin and I want to the use access token which I receive in successful login request, in another request. My Test plan looks like as below
Thread Group : [A]
|- HTTP Sampler - Login Page
|-Regular Expression Extractor [getToken]
|-HTTP Sampler - Other Page
|-Beanshell PreProcessor[Set Header in Authorization]
Regular Expression Extractor parameters and values like below :
Variable Name : token
Regular Expression : {“access_token”:”(.+?)"
Template : $1$
Match No. : 0
Beanshell PreProcessor script like below
import org.apache.jmeter.protocol.http.control.Header;
log.info("Start");
sampler.getHeaderManager().add(new Header("Authorization","Bearer"+vars.get("token")));
log.info(vars.get("token"));
Most probably your Regular Expression Extractor fails as your quotation marks look utterly suspicious. You can double check if the token variable really has the anticipated value using Debug Sampler and View Results Tree listener combination. Also check out jmeter.log file for any suspicious entries, if your Beanshell script fails - the cause will be printed there.
The response data of the Login Page seems to be JSON therefore it makes sense to use the JSON Extractor instead of the Regular Expression Extractor. It allows using JSON Path language in order to extract "interesting" bits of data from the responses. In your case the relevant JSON Path expression would be $.access_token
Since JMeter 3.1 it's recommended to use JSR223 Test Elements and Groovy language for scripting so consider migrating to the JSR223 PreProcessor and Groovy language (you can re-use the same code)
You don't even need the scripting, you can add Authorization header (as well as any other header) using HTTP Header Manager
Could you add debug sampler and try first to confirm your regular expression extractor working as expected? It should provide you the required value of token.
If your token has the required value, I will suggest you to add HTTP Header Manager config element by right clicking on HTTP sampler
HTTP Request => Add => Config Element => HTTP Header Manager
In this config element, you can visually add the Headers as below:
Please Note That:- You have not provided any space/hyphen(-) or between keyword Bear and token.
Refer this link for details :-
https://stackoverflow.com/a/24550552/1115090

How can I dynamically post Request body (xml) and validate the response (xml)?

Is there a way to send the XML request dynamically and validate the XML response?
My scenario is:
I will have a CSV dataset config and inside the csv file I will have two column, the first one is for the inputXMLFilePath and the second column is the expectedXMLResposneFilePath.
So I need to have a JSR233 PreProcessor under HTTP request sampler, read the input file path convert it to the post body, and also has another JSR233 sampler for load the expected response from the expectedXMLResponseFilePath and compare it with the previous XML response. I have a snippet for JSON which is working fine. but for XML how can I do it?
You can use __FileToString() function for both use cases:
To send the XML request body, like ${__FileToString(${inputXMLFilePath},,)} (where ${inputXMLFilePath} is the variable from the CSV Data Set Config)
To validate the response using Response Assertion configured like:
Field to Test: Text Response
Pattern Matching Rules: Equals
Patterns to test: ${__FileToString(${expectedXMLResponseFilePath},,)}
You can use JMeter Functions literally at the any place of your Test Plan so their flexibility is higher than for other test elements. Also JMeter Functions are being compiled into native Java code therefore their execution speed will be higher and footprint will be less comparing to Groovy scripting.
Check out Apache JMeter Functions - An Introduction article to learn more about JMeter Functions concept.

Jmeter JSON API CSRF token value issue

In j meter I getting Access denied for user anonymous however I change json API extractor also.
variable name='token' and j son expression =""$token":"(.+?)""
still unable to do.
Can you please help how and where I have to put j meter extractor?
It appears that your trying to extract the token from JSON and using Regular Expressions is not the best idea as extra space or line break may ruin your test. Since JMeter 3.0 there is JSON Extractor which allows extracting data from JSON responses using JSON Path language. The relevant JSON Path expression will be as simple as:
$..token
With regards to "where to put" - it depends on your application implementation, JSON Extractor obeys JMeter Scoping Rules
If you need to extract the token only once and from only one HTTP Request sampler - put the JSON Extractor as a child of this sampler.
If you need to extract the token from each and every HTTP Request sampler - you need to put the JSON Extractor to be at the same level as HTTP Request samplers.
More information: API Testing With JMeter and the JSON Extractor

How to get HTTP POST request body in Beanshell Preprocessor?

I am facing some trouble using jmeter. This is my use case, I am using CSV data source parameters to construct a HTTP POST request, the request body is read from a CSV column
which contains some placeholders like ${source_id}
I want to replace these placeholders with jmeter parameters which I am initialising through a regex/json extractor(read from the response of last PUT request). I tried using the jmeter variable name in the CSV file but the variable values are not getting substituted. I guess I will have to use the beanshell pre-processor to modify the HTTP POST request body. Can anyone help with the methods I can use to get the HTTP POST request body.
Something like
String requestBody = sampler.getArguments().getArgument(0).getValue();
should help.
sampler is a shorthand to parent sampler class instance, in your case it will be HTTPSamplerProxy, see the JavaDoc for all available methods and fields.
I would recommend considering migration to JSR223 PreProcessor and Groovy language as it is much faster and less resources consuming than Beanshell. Valid Beanshell code will be valid Groovy code so you should be able to convert to JSR223 elements with no or minimal changes. See Groovy is the New Black article for more details.

Resources