Custom message in Simple Data Writer - jmeter

i am using jmeter. I want to use two components: Simple Date Writer. One for success and one for error.
I wonder (I can't find it in the internet) how to attach my own message to the saving file?
For example:
On Success: "YOU ARE THE BEST"
On Error: "Something wrong"
I know, I can add many details about respons to the file, but i need custom message.

You can amend response message based on the condition using JSR223 PostProcessor and Groovy language, something like:
if (prev.isSuccessful()) {
prev.setResponseMessage('YOU ARE THE BEST')
}
else {
prev.setResponseMessage('Something wrong')
}
In the above example prev stands for the SampleResult class instance, see JavaDoc for all available functions and properties and Top 8 JMeter Java Classes You Should Be Using with Groovy article to learn more about various JMeter API shorthands available for JSR223 Test Elements
In this case you can configure the Simple Data Writer to save the response message:
and enjoy the custom message in the result file.

Flexible File Writer Plugin might help you. https://www.blazemeter.com/blog/using-the-jmeter-flexible-file-writer-plugin/
if not then you can open simple data writer results in excel and use conditioning formatting for your requirement.
Hope this might help you.

Related

jmeter functions and variables from csv not substituting

I have a POST request setup in jmeter that has a body like:
"chartId": ${chartid},
"source": "Direct 5",
"documentId": ${__counter},
__counter is a built in function and chartid should be coming from CSV:
However, they are not being substituted in the request. I'm using the latest jmeter (5.5) with Java 8.
Can anyone think of anything I might be missing?
${__counter} is not a built-in function, ${__counter(,)} is. Use Functions Helper Dialog if you're uncertain regarding proper functions syntax.
I cannot comment anything regarding ${chartid} unless I see first 2 lines of your CSV file and the rest of configuration of the CSV Data Set Config, in the meantime you can see all JMeter Variables which are defined with their respective values using Debug Sampler and View Results Tree combination

JMETER: Need to send all entries in a CSV file to HTTP request body in 'one' request

I'm trying to solve a test data issue in Jmeter. Can anyone of you have a look at below problem statement and advise here please ?
Requirement: Need to send all entries in a CSV file to HTTP request body in 'one' request to the end point.
Example CSV File:
"adsfas123wsf00000wqefqwe52145t10000",
"fdfrgvergq120947r0000dwsfqwaef237sadf",
"wfrqwef7865034r78tkahsefjh6985r7asfdaf",
"qefqwe52145t10000adsfas123wsf00000w",
"wsfqwaef237sadffdfrgvergq120947r0000d"
HTTP Request Body:
["${data}"}]
When the data is substituted, I should be able to get below output.
[
"adsfas123wsf00000wqefqwe52145t10000",
"fdfrgvergq120947r0000dwsfqwaef237sadf",
"wfrqwef7865034r78tkahsefjh6985r7asfdaf",
"qefqwe52145t10000adsfas123wsf00000w",
"wsfqwaef237sadffdfrgvergq120947r0000d"
]
Problem Statement: When I use CSV data set config. file, I'm unable to concatenate all entries into one single request body. My understanding is, CSV data set config. the file is not the right option here.
Did some search in StackOverflow and followed a method to achieve above using JSR223 PreProcessor' and the link is, How to send multiple json body using jmeter?.
Followed the above link and tried added below custom code provided.
def builder = new StringBuilder()
new File('/path/to/plans.csv').readLines().each { line ->
builder.append(new File(line).text).append(System.getProperty('line.separator'))
}
sampler.getArguments().removeAllArguments()
sampler.addNonEncodedArgument('', builder.toString(), '')
sampler.setPostBodyRaw(true)
Upon running, I get below error message,
Caused by: java.io.FileNotFoundException,
"adsfas123wsf00000wqefqwe52145t10000",
"fdfrgvergq120947r0000dwsfqwaef237sadf",
"wfrqwef7865034r78tkahsefjh6985r7asfdaf",
"qefqwe52145t10000adsfas123wsf00000w",
"wsfqwaef237sadffdfrgvergq120947r0000d" (The filename, directory name, or volume label syntax is incorrect)
If the file is not found, then how come the entries are read and displayed in the log viewer?
Also, how do I link the output of custom code to the request body? Or is it taken care of by the custom code itself?
You have an error in your code, change this line:
builder.append(new File(line).text).append(System.getProperty('line.separator'))
to this one:
builder.append(line).append(System.getProperty('line.separator'))
If you want to send the full contents of the file you don't even need to go for scripting, you can use __FileToString() function right in the "Body data" tab of the HTTP Request sampler:
${__FileToString(/path/to/plans.csv,,)}
And last but not the least if you need to generate JSON from the plain text it might be a better idea to go for JsonBuilder class, see Apache Groovy - Why and How You Should Use It and Apache Groovy - Parsing and producing JSON
Two steps:
Add a User Parameter Pre-processor before the HTTP request:
Name: DATA
User_1: ${__FileToString(/path/to/plans.csv,,)}
Add the following to request body:
${DATA}

How to update Json file before sending as a request in Jmeter

In my Project, API Contract is changing frequently (every two sprints). From me it is very hard to update the payload in each 'http request' of project test cases.
I am looking for a solution to maintain a separate json file with the request. Update the request json file based on the test cases using Jmeter and send to server.
Do we have any solution for this?
Or please suggest if we have any other approach to handle this case.
sample JSON: original JSON has more than 600 lines.
{
abc:"abc",
xys:"xyz",
"abcd":[{
abc:"abc",
xys:"xyz",
abc:"abc",
xys:"xyz"
PQR:[{
abc:"abc",
xys:"xyz",
abc:"abc",
xys:"xyz",
}]}]
}
I can think of only one option:
Add JSR223 PreProcessor, it will be executed before the request
JSON file can be read using JsonSlurper
At this stage you can amend it according to your needs using Groovy language
Once you done you can construct the JSON back using JsonBuilder and write the generated new JSON back to the file
More information: Apache Groovy - Parsing and producing JSON

How to view the bean shell pre or post processor result

I am trying to use beanshell post processor so I have started with simple http request where I am hitting the google home page. "${url}"=="https://www.google.co.in". After http request page I am adding the BeanShellPostProcessor where in Script area I am using ctx variable and some other code which is like :
int threadNum = ctx.getThreadNum();
String code = prev.getResponseCode();
String message = prev.getResponseMessage();
log.info(threadNum);
log.info("This line has been written by Beanshell Post Processor");
so I have two concerns -
i.The way I am using beanshell is this right ?
ii.Where is the console for beanshell processor in jmeter?
like sample request result can be viewed in listener. I tried with BeanShellListner, but it doesn't show any data.
Also I have kept the "log viewer" on.
You are using it almost right, the only error is that you cannot write an integer to the log file, you need to cast it to String first like:
log.info(String.valueOf(threadNum));
You won't be able to see the result of pre and post processors anywhere apart from jmeter.log file. In case of PostProcessor you can modify parent sampler response data via prev.setResponseData() method where prev is a shorthand to SampleResult class instance
I would also recommend considering switching to JSR223 Elements and Groovy language as this way you will get better performance, out of box support of XML, JSON, etc., and other Groovy's "syntax sugar". See Groovy Is the New Black article for more detailed explanation.

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