Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get all previous sampler results (redirects) in JMeter's JSR223 Assertion

I am hitting one API using JMeter. The URI that I am hitting (Test API-0) will return 302 Found and redirect it to Test API-1 which will again return 302 Found and redirect it to Test API-2. Test API-2 will return 200 OK if everything is fine.

enter image description here

I want to get the protocol, host, path, and response code of Test API-0 , Test API-1, and Test API-2.

In JSR223 Assertion Groovy language, I tried

def url = prev.getURL();
def protocol = url.getProtocol();
def host = url.getHost();
def path = url.getPath();

log.info('Full URL: ' + url.toString())
log.info('Protocol: ' + protocol )
log.info('host: ' + host )
log.info('path: ' + path )

But this will only give me the Test API-2 only (The latest URI only).

I also tried with

log.info("Previous Response URL is: " + ctx.getPreviousResult().getURL());

log.info( "The Sample URL is : " + SampleResult.getUrlAsString() );

Again same result. I only get the Test API-2 only (The latest URI only).

How can I get all Test API-0 , 1 , and 2 ?


[UPDATE 10 Dec]:

The working solution given by user7294900 :

In JSR223 Assertion window

import org.apache.jmeter.samplers.SampleResult;

 SampleResult[] subResults = prev.getSubResults();

   subResults.each { it ->  
   def url = it.getURL();
   def protocol = url.getProtocol();
   def host = url.getHost();
   def path = url.getPath();

	  log.info("URL: " + url )
	  log.info("Protocol: " + protocol )
	  log.info("host: " + host )
	  log.info("path: " + path )
   
   }
like image 261
keylogger Avatar asked Dec 06 '25 03:12

keylogger


1 Answers

You can get also sub results using prev.getSubResults() and get data from the array

 SampleResult[] subResults = prev.getSubResults();

array containing the subresults for this sample

You can iterate each sub results:

subResults.each { it->
   def url = it.getURL());
   def protocol = url.getProtocol();
   def host = url.getHost();
   def path = url.getPath();
}
like image 183
user7294900 Avatar answered Dec 08 '25 12:12

user7294900



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!