Extract Sample start value from Sampler result tab in JMeter - jmeter

I need to extract "Sample start" value means date & time value from the Sampler result tab in JMeter.
Sample Start:2022-07-13 21:56:18 IST
Load time:1549
Connect Time:292
Is there any way to do that? Please suggest some solution.

You can add a JSR223 PostProcessor as a child of the request which start time you want to "extract" and put the following code into "Script" area:
vars.put('start', new Date(prev.getStartTime()).format('yyyy-MM-dd HH:mm:ss zzz'))
it will store the parent Sampler start time into ${start} JMeter Variable
Demo:
More information on what do these prev and vars things mean: Top 8 JMeter Java Classes You Should Be Using with Groovy

Related

I want to fail Beanshell sampler when my script is not conditions not satisfied

I am using Beanshell sampler with java code to compare the two files line by line. I want to fail the Beanshell sampler if comparison failed at any line. I see always my beanshell sampler is sucess in view results treee even the comparison failed or passed. So any one please give me a idea how to fail the sampler.
Note: I used Beanshell Assertion as well but its not worked.
View Results Tree Image
Beanshell Sampler with Beanshell Assertion
boolean differenceInFile = false;
SampleResult.setSuccessful(differenceInFile);
IsSuccess=differenceInFile;
line #2 and line #3 is what you need in your bean shell sampler to pass / fail depending on your file comparasion
Sample JMX File
It will show you as an xml, try to download it
There is SampleResult pre-defined variable which you can use for setting the sampler passed or failed, define response code, response message, response body, etc.
So something like:
if (there_are_differences) {
SampleResult.setSuccessful(false)
SampleResult.setResponseCode("500")
SampleResult.setResponseMessage("Files are different")
}
In the assertion you have AssertionResult for the same.
Also be aware that you should be using JSR223 Test Elements and Groovy language for scripting since JMeter 3.1 so it might be a good option for migration.
More information on JMeter API shorthands available for the JSR223 Test Elements - Top 8 JMeter Java Classes You Should Be Using with Groovy

How to create a Java program in Beanshell PostProcessor in Jmeter to merge all the responses?

I have to send JSON requests based on the CSV test data. Suppose there are 90 records - which are basically the request bodies. I put the Thread in a loop to keep sending the request until the last one in the CSV.
Every time I get the response, I need to append them into a single CSV file. Now, since Jmeter Listener does not consolidate all the responses into CSV (I do not want it in xml), I want to know if I can write a Java snippet in BeanShell, capture all responses and write them to a CSV file.
You can use JSR223 Sampler with File.append adding text with , to append to CSV file
This will append to the end of the file.
File file = new File("out.txt")
file.append("hello\n")
If you want the "program"
Add JSR223 Listener to your Test Plan (since JMeter 3.1 it is recommended to use JSR223 Test Elements for scripting)
Put the following code into "Script" area
new File('myfile.csv') << prev.getResponseDataAsString() << System.getProperty('line.separator')
where prev stands for previous SampleResult class instance. Check out Top 8 JMeter Java Classes You Should Be Using with Groovy article for more information on JMeter API shorthands available for the JSR223 Test Elements.
A better option would be saving a response into a JMeter Variable using i.e. Regular Expression Extractor and writing it to a file via sample_variables property

How to fetch parent url of the current failing url in jmeter?

I am trying to fetch the parent url of the current failing request (for eg consider a travel website ,parent url will be search creteria and current url is selecting flights page).How can we do it .I tried it with putting a if controller after failing url and a beanshell so that it fetches the parent url of all the failings flights request. But what is happening is ,if the current flight request is falining it wont go to the next if controller at all. Execution stops. Can someone guide me with the better way?
First of all forget about Beanshell, you should be using JSR223 Test Elements and Groovy language for scripting starting at least from JMeter version 3.1 (for earlier versions it was also recommended, however Groovy engine wasn't included in JMeter distribution)
Add JSR223 PostProcessor as a child of the 2nd request
Put the following code into "Script" area:
if (!prev.isSuccessful()) {
log.info('Previous sampler name: ' + ctx.getPreviousSampler().getName())
log.info('Previous sampler URL: ' + ctx.getPreviousSampler().getUrl().toString())
}
That's it, you should see the name of the previous sampler and its URL in the Log Viewer panel and in jmeter.log file
You can save the URL into a JMeter Variable like:
vars.put('url', ctx.getPreviousSampler().getUrl().toString())
once done you will be able to access the value as ${url} where required.
References:
prev stands for current SampleResult
ctx is a shorthand for JMeterContext
vars is a shorthand for JMeterVariables
See JavaDoc for the above classes for description of used functions and Apache Groovy - Why and How You Should Use It article for more information on Groovy scripting in JMeter.

Save body request in JMeter

I am using CSV file which contains variables for my XML requests. I know how to see the request sent, but I would like to save each request in a separate file.
Is that possible?
The easiest way seems to be using Flexible File Writer, you can save requestData into a file.
If you need more flexibility - for HTTP Request samplers you can do it with Beanshell Listener like:
Add Beanshell Listener to your Test Plan
Put the following code into the Listener's "Script" area
import org.apache.commons.io.FileUtils;
String data = ctx.getCurrentSampler().getArguments().getArgument(0).getValue();
int threadNum = ctx.getThreadNum();
int loop = ctx.getVariables().getIteration();
FileUtils.writeStringToFile(new File("request-" + threadNum + "-" + loop + ".txt"),data);
You'll get files like request-x-y.txt generated in JMeter's "bin" folder where:
x - JMeter Thread Number
y - Current Loop Iteration (Thread Group level)
See How to Use BeanShell: JMeter's Favorite Built-in Component article for more information on using Beanshell in JMeter tests.
you should save the results as XML and turn on only the "Save Sampler Data (XML)"
Add > Listers> View Result Tree
In result tree click on config button and check the type of data thats has to be stored and provide the link to csv file.
Check the snapshot for more info.
Hope this helps.

In JMeter, how do I save a thread variable in Javascript and reference it from Beanshell?

In JMeter, how do I save a thread variable in Javascript and reference it from Beanshell on the very next step OR the next threadgroup?
I am using the WebDriver sampler plugin and the WebDriver steps seem to be required to be Javascript commands. I wrote a WebDriver code block in Javascript that goes to a website and gets a cookie value.
How do I set the thread-group variable at the end of the Javascript WebDriver step.
How do I retrieve this value from a Beanshell step.
I have tried various things and haven't got anything to work. For example vars.put works in the WebDriver sampler but props.put says 'props' is not defined. The debug sampler shows that my JMeter property was set wonky like this:
"varName"= varName
After setting it in Javascript like this:
props.put( 'varName', varName )
And that doesn't look right AT ALL.
Tricky thing is that vars isn't accessible to WebDriver Sampler as well, you'll need to be more creative. First of all you need to return value as Sampler result as follows:
WDS.sampleResult.setResponseData("My var value is:" + varName);
After all you need to extract this from response with i.e. Regular Expression Extractor using regular expression like:
My var value is: (.+?)
and varName as Reference Name
And finally add Beanshell Post Processor to your WebDriver Sampler with the code like:
props.put("varName", vars.get("varName"));
to convert JMeter Variable to JMeter Property which has "global" scope and can be re-used in other Thread Groups.
The solution I ultimately found was not the same. Using 'WDS.sampleResult.setResponseData' does not work since the WebDriver Sampler code seems to not allow this.
Turns out that I was able to find a workaround using "headers". Using a Beanshell Sampler step right after the WebDriver Sampler, I am able to modify the headers of the previous result (even though I cannot modify the response data itself) and so from the WebDriver Sampler I am able to store the header using 'WDS.sampleResult.setResponseHeaders( "iCookie:" + iCookie )' . Then, from a subsequent Beanshell Sampler (not a post-processor) I am able to get to that value using 'String header = ctx.getPreviousResult().getResponseHeaders();' as long as I stored the header in this format: "HeaderName:HeaderValue" from WebDriver Sampler.
It is really annoying that I cannot do a similar thing using the .setResponseData method. But, I am happy there is a workaround of some kind since I cannot find any documentation anywhere on what the normal process is for this kind of thing.

Resources