0

I have a input data sheet in json format I want to pick the values name and passsword from this sheet and pass it in the request body of a postman request using pre-request script. This is the input sheet :

[
  {
    "num_of_steps": "1",
    "testcases": [
      {
        "comment": "Login",
        "test case": "1",
        "data": {
          "version": "2.1"
        },
        "Input": {
          "name": "panda",
          "password": "apple123"
        }
      }
    ]
  }
]

So What will be the statement/expression in postman to pick name and password that is to be written in the pre-requuest script, I have tried pm.iterationData.get("name"); but this works only when the values are directly present in the inputsheet without any branches.

1 Answers1

0

You can use the Newman command options in here

-d, --iteration-data [file] : Specify a data file to use, either JSON or CSV.

#1 Save as data.json

this data, I suppose your data is this object of array format.

[
    {
        "num_of_steps": "1",
        "testcases": [
          {
            "comment": "Login",
            "test case": "1",
            "data": {
              "version": "2.1"
            },
            "Input": {
              "name": "panda",
              "password": "apple123"
            }
          }
        ]
    },
    {
        "num_of_steps": "2",
        "testcases": [
          {
            "comment": "Login",
            "test case": "1",
            "data": {
              "version": "2.1"
            },
            "Input": {
              "name": "monkey",
              "password": "strong_password1234"
            }
          }
        ]
    }
]

#2 Set global variable

To assign in Pre-request Script tab of Postman

pm.globals.set("jsonBody", JSON.stringify(pm.iterationData.toObject()));
var jsonData = JSON.parse(pm.globals.get("jsonBody"))
var input = jsonData["testcases"][0]["Input"];
pm.globals.set("username", input?.name);
pm.globals.set("password", input?.password);

Will set Global Variable the username and password after POST call.

enter image description here

#3 Access variable in input body

In input Body of POST call

{
    "username": "{{username}}",
    "password": "{{password}}"
}

enter image description here

#4 Setup test in Tests tab of Postman

var jsonData = JSON.parse(responseBody);

var username = jsonData["username"]
var password = jsonData["password"]

pm.test('Check username = ' + JSON.stringify(username), function () {
    pm.expect(username).to.equal(pm.globals.get("username"));
});

pm.test('Check password = ' + JSON.stringify(password), function () {
    pm.expect(password).to.equal(pm.globals.get("password"));
});

enter image description here

#5 Export collection

enter image description here

enter image description here

#6 Demo server by Flask of Python

It will play-back from POST of input body to output body

Save as the server.py filename.

from flask import Flask, jsonify, request

app = Flask(__name__)

@app.route("/user", methods=["POST"])
def post_test():
    content_type = request.headers.get('Content-Type')
    if (content_type == 'application/json'):
        json = request.get_json()
        return json, 201
    else:
        return 'Content-Type not supported!'

if __name__ == "__main__":
    app.run(debug=True)

#7 Install flask and run server

pip install flask
python server.py

enter image description here

#8 Run newman with data.json and the collection file.

newman run ./demo.postman_collection.json  --iteration-data ./data.json

You can see the user and password back to output body. Those values come from your JSON object array file.

Result Testing

enter image description here

Bench Vue
  • 5,257
  • 2
  • 10
  • 14
  • Actually I can't convert into single json and provide it in the pre request script, I need to used a external json file as a input data sheet so whenever there's a changes change I could just edit the input sheet instead of the postman collection. So is there a way/expression so that I could pick the variables from the external json sheet. Thank you! – sleepingpanda Aug 09 '23 at 04:49
  • @sleepingpanda, OK, I update my answer with `--iteration-data` for accessing JSON file. – Bench Vue Aug 09 '23 at 10:18
  • Sorry, One more doubt I only need to pick the username and password from that input json sheet to pass in the request body, so what will be the code in pre request script to only pick username and password from json file – sleepingpanda Aug 10 '23 at 05:21
  • OK, I updated again only use the `username` and `password`. – Bench Vue Aug 10 '23 at 10:49