How to replace a string in a variable that came from a CSV Data Config? - jmeter

In my JMeter test plan, I have two CSV Data Config elements.
CSV Data Config-element-requestBodies: This CSV Data Config element has a variable named "requestBody". In the corresponding CSV file, each line has an XML request with a placeholder string.
CSV Data Config-element-subject: variables. This CSV Data Config element has a variable name "subjectDn". In the corresponding CSV file, each line has a distinguished name (DN) for a user.
The "requestBody" variable will contain an XML request from the CSV file that has the request bodies, with a placeholder string and I want to replace that placeholder with the contents of the "subjectDn" variable, and then use the modified "requestBody" variable in the HTTP Request element.
In other words:
Replace the placeholder string in the ${requestBody} with the value in the ${subjectDn}, then
Use the modified ${requestBody} in the HTTP Request
How can I do that? What is the easiest/least overhead way of doing that?
I've tried using the "__evalVar" function (see https://am.net/lib/tools/NetworkManagement/apache-jmeter-2.8/printable_docs/usermanual/functions.html), but it looks like that is not able to do the replacement when the string is from a CSV file?
Thanks in advance!
Jim

Wrap your ${requestBody} into __eval() function, it will allow JMeter to resolve JMeter Functions and Variables which are present as "placeholders" in the CSV file to their respective values.
${__eval(${requestBody})}
Also if you consider scripting to accomplish your requirements be informed that starting from JMeter 3.1 it's recommended to use JSR223 Test Elements and Groovy language. More information: Apache Groovy: What Is Groovy Used For?

I was able to get this woring by using a Beanshell pre-processor and some Java code that does var.get() and then Java String replaceall() and then var.put().

Related

Jmeter - Need to check empty tags in SOAP request

I have a SOAP request. I need to use jmeter to test multiple requests for same method, each time passing one missing element.
How can I achieve this ?
I tried writing the tags into csv using CSV data config , but I am unable to enter empty values in the csv, each time for a different tag in each csv row.
Any help would be appreciated. Thank you.
The CSV Data Set Config will read the value from next line only on the next iteration of the thread (virtual user), you cannot read more than one line within the bounds of a single iteration. You might want to take a look at __CSVRead() function where it's you who decides when to proceed to the next line.
If the __CSVRead() function isn't applicable for your use case be aware that you can build a request body, i.e. XML from CSV file using JSR223 PreProcessor and Groovy language.
See Creating XML chapter of Groovy documentation for more details.

how to pass the dynamic filename in csv data set config in jmeter while the dynamic names are generated to save data of previous request?

I have one http request whose response comes in nested json and using groovy i am saving that data in different csv file on the basis of conditions.
the name of csv file is generated dynamically and saved in a variable
using vars.put() function
vars.put("_cFileName",cFileName.toString());
when try to use this variable in csv data set config
enter image description here
getting error message
2022-01-19 16:58:39,370 ERROR o.a.j.t.JMeterThread: Test failed!
java.lang.IllegalArgumentException: File ${_cFileName} must exist and be readable
it is not converting the name to actual file name
But in case if file name is not dynaamic and the variable is defined under user defined variable in test plan it will able to convert to actual file name?
is there any way we can use the dynamic name created in an previos request post processor?
You cannot, as per JMeter Test Elements Execution Order Configuration Elements are executed before everything else, CSV Data Set Config is a Configuration Element hence it's being initialized before any JMeter Variables are being set.
The solution would be moving to __CSVRead() function, JMeter Functions are evaluated at the time they're being called so it's possible to provide dynamic file name there, see How to Pick Different CSV Files at JMeter Runtime guide for more details if needed.

Jmeter how to read file path within csv file

Is it possible to pass in file path or read from another file within csv? My test setup is something like that.
Jmeter Test
Http request -> body data -> "items": "${__property(${items})}",
CSV data config
Id,Name,Items
1,MyName,\input\items_json.txt
I want to include a file in csv items column and in jmeter test, it will read and post items json.
Got it working with this.
"items": ${__FileToString(${__property(${Items})},,)},
Not really, being a Configuration Element CSV Date Set Config is initialised before anything else so you will have to consider another approach, i.e.:
__CSVRead() function
__StringFromFile() function
__FileToString() function()
Functions are being evaluated exactly where they're called so this way you can use them for parameterization. See Apache JMeter Functions - An Introduction to learn more about JMeter Functions concept

How to do parameterization for file name text of csv data set config in jmeter

I am passing the value for file name text filed in csv data set config as ${filename}, this variable contains entire path and fetching it form one more csv data set config which is present immediate to the thread group and the variable which I am passing to csv data set config that is inside the if controller here I couldn't able to pass that value to file name text filed, getting problem.
CSV Data Set Config cannot use JMeter Variable as the "Filename". However it is possible to use __property function there.
So your test plan should look as follows:
Read some value into a JMeter Variable i.e. foo via 1st CSV Data Set Config
Convert foo variable into foo property via __setProperty function
Use ${__property(foo,,)} as a "Filename" in 2nd CSV Data Set Config.
For more information on different JMeter's functions see How to Use JMeter Functions post series

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