Search code examples
postmanpostman-testcase

Automation in Postman - Different set of input datasets


Suppose my input body is the below in JSON format

[
    {
        "username": "Test User 1",
        "rollNo": 45
    },
    {
        "username": "Test User 2",
        "rollNo": 46,
        "hometown": "XYZ"
    },
    {
        "username": "Test User 3",
        "rollNo": 47,
        "location": "ABC"
    }
]

How to automate this set of data in POSTMAN . I need help in writing a script where the collection runs 3 times each time takes the input from 1 of the above mentioned values in POSTMAN.


Solution

  • You can do this but using each object from that datafile, as the request body.

    Add the following items to the request, these with be used to get and use the data.

    In the request body:

    {{jsonBody}}
    

    Request Body

    In the Pre-request Script:

    pm.variables.set('jsonBody', JSON.stringify(pm.iterationData.toObject()));
    

    Pre-request Script

    Ensure that the request is saved (no orange dot in the request tab), open the Collection Runner and select the Collection. Select your JSON data file (The iteration count will match the number of objects in the file) and check the Save responses box.

    Run the Collection and each request should use the whole data object for that iteration.

    Collection Runner