3

I am trying to automize a registration scenario in postman using test scripts I have the following JsonArray as a response:

[
    {
        "id": 1,
        "name": "user_A",
        "cntkp": "martin",
        "company": "kreativ",
        "tel": "12345678",
        "email": "user_A@gmail.com"
        "street": "str. 0001",
        "city": "DEF",
    }
    ......
    ......
    ......
    {
        "id": 4,
        "name": "user_B",
        "cntkp": "martin",
        "company": "kreativ",
        "tel": "12345678",
        "email": "user_B@gmail.com"
        "street": "str. 0002",
        "city": "NJ",
    }
    ......
    ......
    ......
    {
        "id": 10,
        "name": "User_C",
        "cntkp": "martin",
        "company": "kreativ",
        "tel": "12345678",
        "email": "user_C@gmail.com"
        "street": "str. 0003",
        "city": "ABC",
    }
    ......
]

the array length can be dynamic and changed (in this sample is 10) and want to find the object with special email (somewhere in the array) and then get the ID from that object and make the assertion based on JsonData from this object (catch elements e.g. check name).

how can I do that?

thanks for the support.

I send a GETrequest to get all Data from Registration DB. as response I get a JsonArray from Json Array I need the specific Object for the assertion (e.g. object with email user_B in sample) . I know my Email address and base on it I have to findout the ID from Object . I can do it when I know which ID is my ID but in case it is dynamic I don't know how to search an array for it in postman to get ID

For example, to assert the company name

pm.expect(jsonData[0].company).to.equal(pm.environment.get("regDB_new_company"))

but if I dont know the ID ( only know my email) I have first to find out the ID of Object then I can asser it.

e.g. in this case first, find the object with email "user_B@gmail.com" then from that object get ID element (in this case 4) then I want to assert for all data from the object

Danny Dainton
  • 23,069
  • 6
  • 67
  • 80
AKADO
  • 481
  • 1
  • 11
  • 19
  • I've commented before on your questions - This isn't a code writing service. You need too, at the very least, show what you have done already to achieve this. This is not a Postman specific problem - It's a understanding of JSON array's and objects. – Danny Dainton Feb 12 '18 at 12:08
  • in one request I get only one JsonData from DB and save this in a variable e.g. – AKADO Feb 12 '18 at 12:12
  • What request? Don't add comments about what you have done - Update the question. Show your workings. – Danny Dainton Feb 12 '18 at 12:14
  • Sorry Danny , I am new in Postman . I have update the question – AKADO Feb 12 '18 at 12:34
  • Being new to Postman is not the issue. Always happy to help out if I can but it's not a site to say 'I want to do this - tell me how'. You need to add what you've tried already to achieve this to the question so it doesn't just sound like you're asking someone to write your solution for you. – Danny Dainton Feb 12 '18 at 12:52

1 Answers1

4

Thanks Danny, I found the solution

var arr = pm.response.json()

for(i = 0; i < arr.length; i++) {
    if (arr[i].email == "userB@gmail.com") {
        pm.environment.set("personID", arr[i].id)
    }
}
Danny Dainton
  • 23,069
  • 6
  • 67
  • 80
AKADO
  • 481
  • 1
  • 11
  • 19