1
votes

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 )
   
   }
1

1 Answers

1
votes

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();
}