Verify dynamic variable and body value in Test

Have variable name {{userName}} which i capture in pre-script as pm.environment.get("userName");

The printing works fine but Assertion error

pm.test("Check For legal name", function () {
    var jsonData = pm.response.json();
    pm.expect(jsonData.Detail.legalName).to.eql(pm.environment.get("userName"));  
});

but gives Assertion error as
AssertionError: expected undefined to deeply equal ā€˜Test Firsnameā€™

What is the syntax to make this test pass. @danny-dainton

I love being tagged in things but there are more people who are able to help you on this Community forum. :smiley:

Without showing people what the response body is for that request - No one will know what jsonData.Detail.legalName is supposed to be referencing.

1 Like

Sorry Danny :slight_smile: i was just going to post itā€¦ i am printing wrong array here is the body.

"Status": true,
    "Message": "",
    "Detail": [
        {
            "legalName": "Test Firsname",
            "Surname": "Danny",
            "address": {
                "streetAddress": "Dr Helpful",
                "city": "Flower test",
                "state": "NY",
}
]

i think it should be

Detail[0].legalName

This works ā€¦ Let me know its ok now?

pm.expect(jsonData.Detail[0].legalName).to.eql(pm.environment.get("userName"));

Only you would know if thatā€™s OK now :joy:

If it solves the issues and your test is passing - I think youā€™re good. :trophy:

1 Like

Well Said :smiley: its ok ā€¦also verify with wrong test using surnameā€¦ it fails so OK now and Sorry i tagged youā€¦ since you always answers first, reason for tagging you.

1 Like

@gpub1 Good Afternoon, I just want to know how you capture username in the environment variable using pre-script. I am new in Postman so I want to know your way.

@kishan-panchal

I would recommend going through the Postman training first, as most of these elements are covered and it will give you an understanding when you read other users solutions.

This is potentially covered in the Learning Centre.

Postman Galaxy Training | Postman API Network

Run through the ā€œAPIs 101 Trainingā€, then the ā€œTesting and Automation Trainingā€ course.

Then if you are still unsure, please post what you have tried, and we may be able to guide you.

In the solution above, the posters username is an environment variable.

Using variables | Postman Learning Center