Upload a File - while running the collection (you need not mention any static location/file, instead you can upload the file(s) from desired location)

  1. Place the files you want to upload in a location.

    Say, I want to upload below two json files from C:\here

    prod.json
    str.json

  2. Create a csv data file ( file.csv ) and place the file names you want to upload as shown below

Files

  1. In your Pre-request script, add set a variable as shown below

pm.environment.set(“filepath”,data._filepath);

  1. Open your collection.json file and you will find the below formdata where you have selected the file location as shown below

“method”: “POST”,
“header”: ,
“body”: {
“mode”: “formdata”,
“formdata”: [
{
“key”: “File”,
“type”: “file”,
“src”: “/C:/here/prod.json”
}

here, modify “src”: “/C:/here/prod.json” to “src”: “{{filepath}}”

  1. Now navigate to the path where you have the files and want to upload. So, in my case I navigated to C:\here and launched cmd from here and executed below command.

newman run --insecure <collection_path>\Products.postman_collection.json -d \file.csv -r htmlextra --reporter-htmlextra-export “<Report_path>\Fileupload.html” --reporter-htmlextra-title “<Report_Name>”

Now my test will run with two iteration as shown below

Iteration1 - you will find the Request body as

{
“File”: “prod.json”
}

Iteration2 - you will find the Request body as

{
“File”: “str.json”
}

Hope, this solution helps us in not selecting a static file and upload all the desired files from selected location

1 Like