首页 > 解决方案 > 在 JMeter 的 JSR223 断言中获取所有以前的采样器结果(重定向)

问题描述

我正在使用 JMeter 访问一个 API。我点击的 URI (Test API-0) 将返回 302 Found 并将其重定向到 Test API-1,Test API-1 将再次返回 302 Found 并将其重定向到 Test API-2。如果一切正常,测试 API-2 将返回 200 OK。

在此处输入图像描述

我想获取Test API-0 、 Test API-1 和 Test API-2的协议主机路径响应代码。

在 JSR223 Assertion Groovy 语言中,我尝试过

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 )

但这只会给我测试 API-2(仅限最新的 URI)。

我也试过

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

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

同样的结果。我只获得测试 API-2(仅限最新的 URI)。

如何获取所有 Test API-0 、 1 和 2 ?


[12 月 10 日更新]

user7294900 给出的工作解决方案:

在 JSR223 断言窗口中

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

标签: groovyjmeterjsr223

解决方案


您还可以使用prev.getSubResults()获取子结果并从数组中获取数据

 SampleResult[] subResults = prev.getSubResults();

包含此样本的子结果的数组

您可以迭代每个子结果:

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

推荐阅读