每次在Jmeter BeanShell中如何循环HTTP请求并更新变量 [英] How to loop an HTTP request and update the variables each time in Jmeter BeanShell

查看:436
本文介绍了每次在Jmeter BeanShell中如何循环HTTP请求并更新变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个HTTP请求:一个请求从api获取数据,另一个请求将数据发布到api.

I have 2 HTTP request: One to GET data from an api and the other to POST data to the api.

GET请求将多个用户带入JSON. POST请求要求每个用户1个请求.因此,我需要:

The GET request brings several users in JSON. The POST request requires to 1 request per user. Hence I need to:

  1. 根据用户数量多次循环同一POST请求(已经通过使用while控制器来检查JSON响应中的用户数量).

  1. Loop the same POST request several times depending on the number of users (already did this by using a while controller that checks the number of users from the JSON response).

对于每个POST请求,我都需要根据JSON响应中用户的信息来更新该请求中使用的变量.

For each POST request, I need the variables used in such request to be updated depending on the user's information inside the JSON response.

我正在尝试的方法是在POST请求中使用BeanShell PreProcessor,但是我遇到了麻烦.

The approach I am attempting is to use BeanShell PreProcessor inside the POST request but I'm having troubles with it.

假设在POST的请求正文中一个变量称为$ {name}.我正在使用JSON Extractor PostProcessor(在GET请求上)路径:"Travelers [0] .FirstName",它返回第一个用户的名字,但随后我需要第二个用户的名字(Travelers

Suppose one variable is called ${name} in the request body of the POST. I am using a JSON Extractor PostProcessor (On the GET request) path: "Travelers[0].FirstName" that returns the first name of the first user but then I need the second user's name (Travelers1.FirstName) to be assigned to the same variable ${name} before the POST request is sent and so on with every single user.

  • 我想制作一个for循环:

  • I want to make a for loop like this:

for (int i = 0; i <= numberOfTravelers; i++) {
 vars.put("Name", Travelers[i].FirstName) 
 }

问题是我不知道如何从另一个先前请求的JSON响应中调用JSON路径.有没有一种方法可以将jsonpath引用到特定的JSON响应,或者将整个JSON响应保存在变量中,然后在该变量中找到特定的值作为JSON路径.

The problem is that I do not know how to call a JSON path from a JSON response of another previous request. Is there a way to reference the jsonpath to the specific JSON response or a way to save the whole JSON response in a variable and then find the specific value inside that variable as a JSON path.

我已经使用JSON提取器尝试过此操作,但是问题是,如果我使用路径:Travellers [*].FirstName,它将实际上获取JSON上的所有名称,但是变量$ {name}仅存储一个名称,而不是全部作为一个数组,以后我可以使用普通变量$ {name [i]}在for循环中访问它们.这就是为什么我需要从BeanSheall访问JSON路径的原因.

I have tried this with the JSON extractor but the problem is that if I use the path: Travelers[*].FirstName, it will actually get all the names on the JSON but the variable ${name} will only store ONE name, not all of them as an array that I can later access in my for loop with a normal variable ${name[i]}. That is why I need to access the JSON path from the BeanSheall.

以下是JSON响应的示例:

Here a sample of the JSON response:

{
"Travelers":
[
    {
        "FirstName":"VICTOR",
        "Surname":"ORREGO",
        "Key":"1.1",
        "PassengerRPH":1,
        "TypeCode":"ADT"
    },
    {
        "FirstName":"JULIO",
        "Surname":"OZAETA",
        "Key":"2.2",
        "PassengerRPH":2,
        "TypeCode":"ADT"
    }
]
}

这是我正在使用的GET请求中的PostProcesor JSON提取器 .当前正在将其从JSON响应(Victor)获得的名字分配给变量$ {Name} 我需要在(POST请求的)下一次迭代中使用变量$ {Name}来返回该路径中的下一个名称,即Julio.

This is the PostProcesor JSON Extractor at the GET request that I'm using. it is currently assigning the first name it gets from the JSON response (Victor) to the variable ${Name} I need that in the next iteration (of the POST Request) the variable ${Name} to return the next name in that path, which is Julio.

推荐答案

这是解决方案.

  1. 向获取请求添加 JSON提取器. -1来存储所有名字,如下所示.
  1. Add a JSON Extractor to the get request .. use match no -1 to store all Firstnames as show below.

我正在提取所有名字,并使用单个JSON提取器将其存储在JMeter变量中 2.在相同的获取请求中,添加 JSR223后处理器并设置计数器值设为1

i'm extracting all Firstnames and storing it in JMeter variables with a single JSON extractor 2. To the same get request add a JSR223 Post processor and set the counter value to 1

vars.put("counter","1");

  1. 在测试计划中添加 while循环并将以下条件添加到while循环中.
  1. Add a while loop to the test plan and add the following condition to the while loop.

$ {__ javaScript(parseInt($ {counter})< = parseInt(vars.get("FirstName_matchNr")),)}

${__javaScript(parseInt(${counter})<=parseInt(vars.get("FirstName_matchNr")),)}

4.向发布请求中添加 JSR223预处理器并添加以下代码

4.To the post request add a JSR223 Pre Processor and add the following code

vars.put("name",vars.get("FirstName_"+vars.get("counter")));

这会将FirstName_Matchno的值存储在名称变量中.

This will store FirstName_Matchno's value in name variable.

  1. 在POST请求中添加 JSR223后处理器并增加计数器
  1. Add a JSR223 Post Processor to the POST Request and increment the counter.

int计数器= Integer.parseInt(vars.get("counter"))+1; vars.put("counter",Integer.toString(counter));

int counter = Integer.parseInt(vars.get("counter")) +1; vars.put("counter",Integer.toString(counter));

您可以在结果中看到其在每次循环迭代中都使用不同的名称

You can see in the results its substituting a different name on each iteration of loop

让我知道是否有帮助.

这篇关于每次在Jmeter BeanShell中如何循环HTTP请求并更新变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆