Postman Tutorial Part 46 – Accessing Data Variables in Pre-request and test scripts

We have already learnt to create parameterized request body by reading data from a CSV and JSON file. If you have not gone through them yet, you must go now.

Creating parameterized request by reading data from a CSV

Creating parameterized request by reading data from a JSON

Achieving Data driven testing using CSV in Postman

Achieving Data driven testing using JSON in Postman

We can store and use data from csv and json for validation or expected results as well. For this we must be able to read those data (variables) in scripts. We will going to learn the same in this post.

Postman Sandbox provides an API reference named “pm“. “pm.iterationData” API can be used to play around data variables. This iterationData object contains data from the data file provided during a collection run.

You must be thinking that there will be multiple rows in csv or JSON Objects in JSON, so which row data it will return? It is a valid question. We know that mostly CSV or JSON data files are used to achieve data driven testing of API in Postman. Each row is considered as an iteration. So iterationData Object always contains data from currently executing iteration row.

To get value of a data variable, we need to use get() method as below:-

pm.iterationData.get(variableName)

Example:-

Let’s add below codes in to Pre-request Script and Tests for API used in Creating parameterized request by reading data from a CSV . You can use example of JSON as well.

Let’s run above API using data file with one row and multiple rows :-

For data file with single row:-

For data file with multiple rows:-

Please note all above concepts are same for both CSV and JSON data files.

You can find all Selenium related post here.
You can find all API manual and automation related posts here.
You can find frequently asked Java Programs here.

1 thought on “Postman Tutorial Part 46 – Accessing Data Variables in Pre-request and test scripts

Leave a Reply

Your email address will not be published. Required fields are marked *