Jmeter use random string inside CSV file and resolve at runtime - csv

I have CSV File where I have stored full JSON Request and using this variable in API RQ - ${Request}
inside each row of the CSV File I have added ${randomVariable}
In my test plan I use
randomVariable ${__RandomString(10,QWERTYUIOPASDFGHJKLZXCVBNM4563456345634_,)}
this generates the random variable but in the JSON instead of actual random value its passed as ${randomVariable}
I have tried using Beanshell PreProcessor with get and put but still doesn't work. Please help.

If you want JMeter to evaluate the variables which are coming from external data sources, i.e. CSV files, you need to wrap the variable reference name into __eval() function, to wit:
${variableFromCSV} - will return ${randomVariable}
${__eval(${variableFromCSV})} - will return the actual value of the ${randomVariable}
Demo:
More information: Here’s What to Do to Combine Multiple JMeter Variables

Related

I am trying to add a username and string combined to a variable in Jmeter [duplicate]

I have a requirement to use randome url from a url list I extract from a json response.
Say I extract them in this mannser
imageUrls_1=https://blah01.com
imageUrls_2=https://blah02.com
imageUrls_3=https://blah03.com
imageUrls_4=https://blah04.com
imageURLs_matchNr=4
In a following JSSR223 sampler I was able to generate a variable called "url" with one of the url names selected randomely
("imageUrls_1","imageUrls_2",etc)
I was thinking to use them in my HTTP request to get the correcponding url as follows. ${${url}}. But soon found out its not giving me anything other than "${${url}}" :(.
JMeter Is it possible to place a varibale inside a varible name?
Basically I need to use one of the extracted urls randomely in my HTTP request.
The easiest way is going for __V() and __Random() functions combination like:
${__V(imageUrls_${__Random(1,${imageURLs_matchNr},)},)}
Demo:
More information: Here’s What to Do to Combine Multiple JMeter Variables
Use __V function
${__V(url)}
The V (variable) function returns the result of evaluating a variable name expression.

how to Create multiple json object & post in single request

I have my csv file as:
row 1 - 1000 uuids & row2 - 1000 names
i need to call each uuid & their corresponding name in below json structure and finally need to post in single request.
so far i have tried below json in jsr223 preprocessor & response is successful:
import groovy.json.JsonBuilder
def builder = new JsonBuilder()
builder({
Data{
"${__UUID()}"{
name{
"${__RandomString(10,abcdefg)}"(1)
}
p([1,1])
m([1,2])
o([2,3])
}
}
E([])
S(${__time()})
})
println builder.toPrettyString()
vars.put("json", builder.toPrettyString());
Now the problem is i need to link line 6 ("${__UUID()}) & line 8 (${__RandomString(10,abcdefg)}) with csv file & need to call all the columns, which i haven't known how to proceed & finally post as single request.
Any help would be greatly appreciated. Thanks.
You can fetch the arbitrary cell from the CSV file using __CSVRead() function, check out How to Pick Different CSV Files at JMeter Runtime article for more details.
Not knowing the format of the JSON payload you're trying to generate it is not possible to come up with the comprehensive solution, but one thing is obvious, as per JSR223 Sampler documentation:
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.
So don't ever inline JMeter Functions or Variables into JSR223 scripts as you do because it will update only once, on subsequent iterations the value will be cached and will not update.

How to use csv to provide different values for json assertion for different inputs in Jmeter?

I am relatively new to API Testing.
Currently I have a setup where I am having different request jsons and for each request json there is different repsonse json assertion configuration provided with the appropriate values in Jmeter.
Since my json structure will be same and only the input values will be different, I am thinking to generalize the input values using csv and keep only one request configuration.
But would I be able to provide different values(like jsonpath and expected value) to one response json assertion configuration using csv? Because the jsonpath and expected value both will depend upon the input provided and it can have major difference as per the case.
If yes, then how to do it please let me know.
Also if I can achieve my use case using other free API Testing Tool like Postman then also let me know.
You can normally parameterize any JMeter Test Element using CSV Data Set Config
For example you have the following response:
{ "name":"John" }
And the following CSV file:
$.name,John
$.name,Jane
Add CSV Data Set Config to your Test Plan and configure it like:
Add JSON Assertion as a child of the request which returns the above JSON and configure it like:
That's it, each virtual user and/or iteration will pick up the next line from the CSV file and the ${jsonPath} and ${name} placeholders will be replaced with their respective values:
as you can see, the first request passed because name matched John and the second failed because the assertion expected name to be Jane and got John

passing variable to json file while matching response in karate

I'm validating my response from a GET call through a .json file
match response == read('match_response.json')
Now I want to reuse this file for various other features as only one field in the .json varies. Let's say this param in the json file is "varyingField"
I'm trying to pass this field every time I am matching the response but not able to
def varyingField = 'VARIATION1'
match response == read('match_response.json') {'varyingField' : '#(varyingField)'}}
In the json file I have
"varyingField": "#(varyingField)"
You are trying to use an argument to read for a JSON file ? Sorry such a thing is not supported in Karate, please read the docs.
Use this pattern:
create a JSON file that has all your "happy path" values set
use the read() syntax to load the file (which means this is re-usable across multiple tests)
use the set keyword to update only the field for your scenario or negative test
For more details, refer this answer: https://stackoverflow.com/a/51896522/143475

Using Jmeter, I need to add UUID extracted from JSON in CSV in same column (multiple values of UUID) So to pass in Delete Path

Using Jmeter, I need to add UUID extracted from JSON and add that in CSV in same column (multiple) to feed in Delete Request (REST). This is to test multiple delete calls which has unique UUID generated from POST call. Or is there any other way I can test multiple delete call after extracting from POST calls. Lets say 50 Post then 50 Delete calls.
I don't think you need to do anything as given threads reside in the same Thread Group you should be able to use the same variable for the Delete request.
JMeter Variables are local to a thread so different threads will have different variable values.
If you are still looking for a file-based solution be aware of fact that you can write an arbitrary JMeter Variable into a file using Groovy code like:
Add JSR223 PostProcessor after the JSON Extractor
Make sure you have groovy selected in the "Language" dropdown
Make sure you have Cache compiled script if available box ticked
Put the following code into "Script" area
def csvFile = new File('test.csv')
csvFile << vars.get('your_variable')
csvFile << System.getProperty('line.separator')
This way you will get any extracted UUID written into test.csv file in JMeter's "bin" folder, you can use it in the CSV Data Set Config for your Delete request.
More information:
Groovy Goodness: Working with Files
Apache Groovy - Why and How You Should Use It