2

I have request with number of tests cases, same endpoint, different actual values, different expected error messages. I would like to create parameterized request sending particular value and check particular error message from list with all of the cases. Request body:

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

}

Response:

{
"error_message": "{{error_message}}",
"error_code": "{{error_code}}"
}

Error message changes due to different cases:

  1. Missed username
  2. Missed password
  3. Incorrect password or username
  4. etc

Now, I have separate request on each case. Question:

Is there way have 1 request with set of different values, checking particular error messages/codes?

Vova
  • 3,117
  • 2
  • 15
  • 23
  • 1
    Thats how you do data driven testing , use a csv or r json and run your script – PDHide Mar 18 '21 at 19:36
  • I'll let @PDHide leave a full answer for this but it's exactly as he mentioned. Basically, create a CSV file with the variable names as the headings and the values for each of the tests on new lines under the headings. Use that data file in the Collection runner. – Danny Dainton Mar 18 '21 at 20:17
  • @DannyDainton sure, Thank you danny was waiting to know whether that was what ops wanted as its a straight forward use case . As danny mentioned postman has a really rich documentation that you can make use of https://learning.postman.com/docs/running-collections/working-with-data-files/ – PDHide Mar 18 '21 at 20:26

2 Answers2

1

Create a csv:

username,password,error_message,error_code
username1,password1,errormessage1,errorcode1
username1,password1,errormessage1,errorcode1

Now use this as data file in collection runner or newman.

variable name is same as the column name and, for each iteration you will have corresponding row-column value as the variable value. Eg for iteration1 username will be username1

. As danny mentioned postman has a really rich documentation that you can make use of

https://learning.postman.com/docs/running-collections/working-with-data-files/

PDHide
  • 18,113
  • 2
  • 31
  • 46
  • it really looks great. But I have to store additional file with my collection and env. Does it have opportunity to resolve it inside test request? – Vova Mar 18 '21 at 21:13
  • Could you make it more clear . i couldn't understand the comment – PDHide Mar 18 '21 at 21:22
  • 1
    I mean, is there way implementing it without additional files?just collection and env – Vova Mar 18 '21 at 22:14
  • Please see the added answer – PDHide Mar 19 '21 at 07:56
  • what if I need running my postman collection in CI through shared workspace without opportunity to export csv file?locally seems like it solves my question – Vova Mar 19 '21 at 08:19
  • See the newly added answer , you can store csv value as environment variable instead – PDHide Mar 19 '21 at 09:30
  • For ci\cd we have to push the csv file also in ideal cases , that's what we do in all frameworks including selenium – PDHide Mar 19 '21 at 09:31
0

Adding another answer on how to run data driven from same request:

Create a environment variable called "csv" and copy the below content and paste it as value:

username,password,error_message,error_code
username1,password1,errormessage1,errorcode1
username1,password1,errormessage1,errorcode1

enter image description here

Now in pr-request add :

if (!pm.variables.get("index")) {

    const parse = require('csv-parse/lib/sync')
    //Environmental variable where we copy-pasted the csv content
    const input = pm.environment.get("csv");
    const records = parse(input, {
        columns: true,
        skip_empty_lines: true
    })

    pm.variables.set("index", 0)
    pm.variables.set("records", records)
}

records = pm.variables.get("records")
index = pm.variables.get("index")
if (index !== records.length) {
    for (let i of Object.entries(records[index])) {
        pm.variables.set(i[0], i[1])
    }
    pm.variables.set("index", ++index)
    pm.variables.get("index")===records.length?null:postman.setNextRequest(pm.info.requestName)
}

Now you can run data driven for that one particular request:

Eg collection:

https://www.getpostman.com/collections/eb144d613b7becb22482

use the same data as environment variable content , now run the collection using collection runner or newman

Output

enter image description here

PDHide
  • 18,113
  • 2
  • 31
  • 46
  • when we re-run that request, shouln't line: pm.variables.set("index", 0) be run each time in nextRequest precondition? – Vova Mar 19 '21 at 10:05
  • 1
    pm.variable creates a local variable , its life time is the complete collection or newman run. So when you start the run you will have index value undefined , and it sets to 0 , then it keeps on incrementing . – PDHide Mar 19 '21 at 10:15
  • when your run complete all variables are destroyed. For next run again the value starts from undefined – PDHide Mar 19 '21 at 10:16
  • @Vova see the updated answer it was setting next reqeust even for last iteration , i added step to check if its last iteration then don#t send next request – PDHide Mar 19 '21 at 17:39