How to loop an HTTP request and update the variables each time in Jmeter BeanShell

早过忘川 提交于 2020-01-16 10:03:11

问题


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

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

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

  2. 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.

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

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.

  • I want to make a for loop like this:

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

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.

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.

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"
    }
]
}

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:


here is the solution..

  1. Add a JSON Extractor to the get request .. use match no -1 to store all Firstnames as show below.

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. 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")),)}

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

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

This will store FirstName_Matchno's value in name variable.

  1. Add a JSR223 Post Processor to the POST Request and increment the 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

Let me know if it helps..



来源:https://stackoverflow.com/questions/50974115/how-to-loop-an-http-request-and-update-the-variables-each-time-in-jmeter-beanshe

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!