How to Map RegEx variables with BodyData fetched from CSV Files - jmeter

CSV Data Config from JMeter contains following -
File.csv Contains following
GetAllOrderItems.txt contains following
{"dataRows":[],"orderGuid":"${orderGuid_1_g1}","facilityPk":"0","jtStartIndex":0,"jtPageSize":100,"jtSorting":"SKU ASC","isMap":"1"}
orderGuid_1_g1 is fetched from Regular Expression extractor
I passed following to JMeter body data
When I execute JMeter test, I can see follwing
{"dataRows":[],"orderGuid":"${orderGuid_1_g1}","facilityPk":"0","jtStartIndex":0,"jtPageSize":100,"jtSorting":"SKU ASC","isMap":"1"}
However, I want ${orderGuid_1_g1} to be fetched from Reg Ex of previous HTTP Request.
I want following
{"dataRows":[],"orderGuid":"644e1dd7-2a7f-18fb-b8ed-ed78c3f92c2b","facilityPk":"0","jtStartIndex":0,"jtPageSize":100,"jtSorting":"SKU ASC","isMap":"1"}
Code works well, If i did not fetch Body data from text and directly insert below code inside body of HTTP Request
{"dataRows":[],"orderGuid":"${orderGuid_1_g1}","facilityPk":"0","jtStartIndex":0,"jtPageSize":100,"jtSorting":"SKU ASC","isMap":"1"}

If your file contains nested JMeter Variables you need to wrap your __FileToString() function call in __eval() function like:
${__eval(${__FileToString(${GetAllOrderItems})})}
Demo:
More information: Here’s What to Do to Combine Multiple JMeter Variables

JMeter does not interpret variables when you load content from a file.
Besides, for a load test using __FileToString function is not advised unless it is used for setup for example which is not the case here.
So just put the JSON Body request in the request body, it will be better for readability.
If you still want to do that, follow Dmitri Answer.

Related

JMeter how to select randomely from a extracted set of values

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 can i generate a coulmn value as a random value in a csv file while doing load test with Jmeter

I am using Jmeter for load testing. I am passing parameters of the body from a csv file for an api. But our application will not receive the duplicate value. So when i load test by multiple users, the request get failed.because i am passing same parameter value for all users.So i need to make some of the columns with an instant value(random). so that different users will get different values.
{
"clinic_name":"clicnic",
"first_name":"provider1",
"initial":"v",
"last_name":"doc",
"salutation":"salu",
"address_1":"testttadd1",
"address_2":"testtttadd2",
"city":"ckdy",
"state":"Arizona",
"zip":"12365",
"fax":"",
"email":[{"email_id":"dfdf#dzcz.in","type":"Work","note":null}],
"phone":[{"number":"1235974444","note":"","type":"Mobile","extension":""}]
}
here i have given the request body and i need different email for different users. How can i do this?Can anyone help me..Thanks in advance.
you can use __RandomString function for the value of email_id:
"${__RandomString(15, abcdefghijklmnopqrstuvwxyz)}#${__RandomString(10, abcdefghijklmnopqrstuvwxyz)}.in"
The easiest is using Faker library like:
Download Java Faker jar and drop it to JMeter Classpath
Restart JMeter to pick the .jar up
You can use the following __groovy() function directly in the HTTP Request sampler body:
${__groovy(new com.github.javafaker.Faker().internet().emailAddress())}
Alternatively you can put the above expression into your CSV file, however make sure to wrap the variable reference into __eval() function, for example if you had ${email} you will need to change it to ${__eval(${email})}

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.

Dynamic variables use with file upload as in the body of the HTTP request JMeter

I am uploading a file [.xml] to be the body of the POSt HTTP request. But the file has a variable ${TEVAM} whose value is being fetched from the previous request and being assigned.
But the value is not being replaced.
What should I use here?
In order to get the nested variables resolved you need to use __eval() function as a wrapper for i.e. __FileToString() function like:
${__eval(${__FileToString(test.xml,,)})}
More information: Here’s What to Do to Combine Multiple JMeter Variables

JMeter using a variable as input for a response assertion pattern

I have a test plan in JMeter that has a Response Assertion where I'd like to use a variable that comes from a CSV Data Set Config. So my Pattern looks like:
${assert1}
Which corresponds (at least in my thoughts) to what comes from the file used in CSV Data Set Config, but it doesn't work. I have seen multiple suggestions to use a Regular Expression Extractor, but the examples I saw refer to something they're trying to capture from a page. In my case I am using an external csv file.
My question: how can I use a variable as input for a Response Assertion pattern ?
I have successfully used a CSV Data Set Config as a source of string that I use in a url parameter, and then use a Response Assertion to seek out that parameter in the response.
Variable name: P.
URL: /product/${P}.
The response assertion Parterns to Test: ${P}.
I wonder if it is not necessary to use the CSV data variable in the request in order for it to be available in the response assertion?
If my understanding is right,
here your problem:
You want to use the value from your CSV as part of URL and that too via variable.
Solution:
Configure your CSV Data Set Config like this:
Filename: url csv path
variable name : assert1
Delimiter : , (if your CSV comma separated)
leave remaining unchanged
That's it you can use the variable assert1 anywhere: ${assert1}.

Resources