-1

我已按以下顺序添加了测试计划

1.Test Plan with user defined variables
2.Header Manager
3.Thread Group 1
4.Http Request
5.JSON extractor
6.Thread Group 2
7.Http Request
8.BeanShell Preprocessor
9.Result Tree

截屏

如何将访问令牌从第一个线程组传递到第二个线程组?

4

3 回答 3

0

Just finished figuring this out. Don't know if it's the best solution. Looked like there were other options.

In my case the first thread group was reading a list of users and passwords from a csv file.

I did it by writing a csv file in the first thread using "JSR223 PostProcessor" after each authentication API call.

Then I read the newly created csv using the "CSV Data Set Config" in the second thread.

Groovy script follows:

import org.apache.jmeter.services.FileServer

log.info("*************************************") 
def userId = vars.get("user_id") //JMeter var from parsing auth request
def authToken= vars.get("auth_token") 
def configDir = vars.get("config_dir") 

log.info("userId:" + userId) 
log.info("authToken:" + authToken) 

def outputFilePath = configDir + "/userToken.csv"

File outputFile = new File(outputFilePath)

//check if the file exists
if (!outputFile.exists()) {
  log.info("File " + outputFilePath + "does not exist")
  log.info("Creating a new file") 
  outputFile << "userId,authToken\n"
}   
    
outputFile << userId + "," + authToken + "\n"

Test Plan on JMeter

于 2022-02-04T10:23:13.660 回答
0

变量不能在线程组之间传递/共享。

在此处输入图像描述

可能有多种解决方案。

选项1

使用 JMeter 属性在线程组之间共享访问令牌。 props.put("accessToke", accessToke)添加令牌并用于props.get('accessToken')从第二个线程组中检索值。

在此解决方案中,您可以在线程组之间仅共享一个令牌。

选项 2

使用线程间通信插件。

这些队列以先进先出的方式工作。您可以从一个线程将字符串值放入队列中,然后从另一个线程获取该值,即使在另一个线程组中也是如此。

于 2021-09-04T16:40:39.360 回答
0

在您上次执行测试期间至少有 3 个错误:

在此处输入图像描述

检查jmeter.log 文件以获取详细信息,原因应该在那里


  1. 您应该使用不同的线程组来代表不同的业务用户组,如果您正在模拟身份验证流程,将两个 HTTP 请求采样器保持在一个线程组下是有意义的

  2. JMeter 变量是线程(虚拟用户)的本地变量,因此您将无法访问不同线程和线程组中的变量值

  3. 从 JMeter 3.1 开始,您应该使用 JSR223 测试元素和 Groovy 语言编写脚本

  4. 根本不需要编写脚本,只需添加一个 HTTP 标头管理器作为workspace请求的子项(请参阅JMeter 范围规则 - 终极指南文章以了解有关 JMeter 测试元素范围的更多信息)并在那里定义令牌。建议的测试计划结构:

    在此处输入图像描述

于 2021-09-05T08:02:21.763 回答