Problem with x-www-form-urlencode + .csv + tests

hi there!
I have 12 post requests. The content type is "application/x -www-form -urlencoded. each one contains one test. When simply running the collection, everything works and the tests pass. but when using the csv file, for some reason, 144 tests are performed with 12 iterations. 15 of them pass. all the variables are global.
this is the example of reqest body -


and this is my csv file (in readable form) -

please tell me in which direction to look for the error? Why am I getting 144 tests?

What do you think should happen?

When you use an data file, it will run each of the 12 requests once for each line in the CSV file. Each line in the CSV file is an iteration.

So if you have 12 lines in your CSV file with 12 requests, then its going to be 144 requests. Therefore if you have 1 test in each of those requests, it will also be 144 tests.

You haven’t mentioned what the test is?

Why global variables, do you really need these values available to all of your collections in Postman?

thank you for your answer. by test I mean just test -


one line of file = one request = one test. i think so.
if i run collection without tests, i have 12 iterations, 12 reqests, that’s why i expect to have 12 test results

I understood what you meant… only now I don’t understand how to run a collection of these 12 requests so that each request corresponds to only one line of the data file

Do you really need 12 requests, or just two, or potentially one.

  • Sale Request with Account Number
  • Sale Request with Track Data

Are all the requests against the same end point with different data?

So it could just be…

  • Sale Request

The aim is to loop though your CSV but you sort of need to know which data points have to change within each request and which might be static through all requests to work out the best way to split things up, and whether it can be done with one request or really does need to be split up into 12 requests.

I’m wondering if it would be possible to have a single request single request with all of the fields that are static, and then use a pre-request script to retrieve the fields from the CSV and then add these to the request.

All of the data for each iteration is held within a special “data” dictionary.

If you just console log the data variable in your pre-request script, you should see an JavaScript object with all of the elements for that iteration.

console.log(data);

To update the body for an urlencoded request, it would be something like the following.

Considering the following basic request to Postman Echo.

In the pre-request script, you can add keys and values to the request.

let elementstoAdd = {"key":"password", "value":"123456"}
pm.request.body.urlencoded.add(elementstoAdd);

Which you can see were added into the request.

image

To loop though the data variable (which should be a single line of your CSV) and update the body, you can do the following…

for (let [key, value] of Object.entries(data)) {
    console.log(key, value);
    if (value) {
        pm.request.body.urlencoded.add({"key": key, "value": value});
    }
}

It will check if a particular column has a value, as I’m assuming one of the issues is that some of the lines in the CSV have data in certain columns and others don’t.

If you were sending the same elements on every request, then you probably don’t need any of this and should be able to just reference them directly as variables using {{data.columnname}} in your body.

The above is only really needed if certain requests have more or less elements that you have to cater for.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.