jmeter prev.getResponseDataAsString getting wrong return - jmeter

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

Related

Unable to extract the data from previous response in jmeter

I have a situation where the request saves the data using PUT method. The dynamic content is webkit(json body). So kindly help me how to capture the value.
Thanks
Roshan
If you want to extract value(s) from the previous response the options are in:
Boundary Extractor - if you provide a JMeter Variable name and empty left and right boundaries - it will store the whole response into that variable
JSON Extractor - allows extraction of partial data using JSONPath query language
JSON JMESPath Extractor - the same as point 2 but uses a different language - JMESPath which is is more "standard" and powerful
More complex extractions/transformations can be done using JSR223 Test Elements and Groovy language
We cannot "kindly help you" more without seeing the previous response and the next request, the only "help" I can provide so far is a piece of information that WebKit is a browser engine so I have a doubt regarding your statement about sending it somewhere.

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!!!!

Unable to pass value from beanshell preprocessor to sampler?

I am trying to set value in beanshell inside "CANCEL ORDER" sampler and then use in sampler request body.
Trying
vars.put("orders",Arrays.toString(orderList.toArray()));
and accessing in json request body using ${orders} and its not passing value.
{
"orderIds": ${orders},
"tonce": "${tonce}"
}
POST data:
{
"orderIds": ${orders},
Your "not passing value" doesn't tell anything to us, if your Beanshell script doesn't work as expected first of all you need to check jmeter.log file for any suspicious entries, if your script fails somehow you will be able to see the error message in the log.
It worth checking your orderList variable value using Debug Sampler or printing it to the aforementioned jmeter.log file using log.info() shorthand
Since JMeter 3.1 it's recommended to use JSR223 Test Elements and Groovy language for scripting as:
Groovy has built-in JSON support
Groovy performs much better comparing to Beanshell

Fetch Javascript variable in source section using Jmeter

I have a series of interconnected pages to test using JMeter. The problem is that the initial page has a Javascript variable in source section which is more of a session variable. This variable is passed in the URL for subsequent pages.
So basically I would like to fetch this javascript variable when I load the initial page from the source section and pass it to next URL(s).
Is there a way I can achieve this using JMeter.
Are you able to see the session variable in the response of initial page?
(in view result tree listener)
If yes, then correlate this value and pass the variable in to next request (use regular expression extractor for fetching the value, still if you are finding some issue in correlating the value than please share the response of first request over here so that I can provide you regx for that)
People mostly Regular Expression Extractor to fetch dynamic values from previous responses, in general the process looks like:
Add Regular Expression Extractor as a child of the request which returns desired data
Use Perl5-style regular expression to match what you're looking for
Provide a template to choose match group - ususally $1$ if you looking for a single value
Provide a reference name to refer the extracted value, i.e. foo
Use extracted value as ${foo} where required
You can visualise JMeter Variables using Debug Sampler and View Results Tree listener combination.
The easiest way to debug your regular expressions is using View Results Tree listener in "RegExp Tester mode"
See How to debug your Apache JMeter script article for more information on troubleshooting your JMeter test.

Anyway to auto set jmeter assertions to initial responses?

I am building out a test suite in jmeter and want to set the assertions to the initial responses that I get from the api calls. Is there any way to do this besides copy pasta?
For clarity each individual call has its own assertion, which for the moment is the response it is receiving.
I want the assertions to be populated by the responses.
If I understood currectly - you are coming up with some JMeter-Baseline script - you also assume that the response you get now is correct - that is what you expect to get it in future as well , so you want to put the response data in the assertion.
If yes, JMeter will not remember the initial responses. So you need to create your script accordingly.
You can have a beanshell postprocessor to write the the responsedata in a CSV file. Later you modify the script to look for the assertion string from CSV file to compare. This is the statement to get the Response Data.
prev.getResponseDataAsString()
As per How to Use JMeter Assertions in 3 Easy Steps guide, JMeter Assertions have their scope.
If you put an assertion at the same level as all requests it'll apply to all of them. See image below for explanation:
I would use a CSV file with the URL and the assertion string you want to match.

Resources