groovy - 在 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 )
}
解决方案
您还可以使用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();
}
推荐阅读
- google-apps-script - 使用从谷歌表格中提取的数据从模板创建谷歌文档文档
- android - 集成使用 Kotlin Coroutines 依赖问题构建的库模块
- python - 使用 PySoundFile,如何获取字节缓冲区?
- r - 如何在 R 中更改 mrset 的列名?
- r - 如何编写一个 for 循环来从包含 NA 的数据集中计算模型残差?
- java - 我的 Exomedia 代码在某些设备上不起作用,我该如何解决?
- php - 如何将从数据库中随机选择的图像放置在页面上的随机位置?
- python - python中用于检测Python中缺失值的用户定义函数?
- java - Android 应用程序客户端与 java 服务器的相互 TLS
- c++ - 如何将字符串推送到 shared_ptr 的向量中?