Postman Test with comparison to global variable

1.9k views Asked by At

I want to orchestrate two requests in Postman. The first response will give me a variable. I save this id to a global variable id. In Postman this variable is usually accessible via {{id}}.

Then I send a second request with this id (like GET foo.bar/{{id}}). Now I want to check, if the id is in the result as well.

This is what I tried in the test code:

    var jsonData = pm.response.json();
    pm.expect(jsonData.id).to.eql({{id}});

where idis the variable from the first response (e.g. 72b302bf297a228a75730123efef7c41).

The response for the second request looks sth. like this:

{
    "id": "72b302bf297a228a75730123efef7c41"
}

Here some examples which did not work either:

    var jsonData = pm.response.json();
    pm.expect(jsonData.id).to.eql("{{id}}");
    var jsonData = pm.response.json();
    var myId = {{id}};
    pm.expect(jsonData.id).to.eql(myId);

My excpectation is, that the test will be positive and the `id from the request will be found in the response.

Do you have an idea how to solve this problem?

Thanks for the help.

1

There are 1 answers

4
Danny Dainton On BEST ANSWER

The {{...}} syntax cannot be used like that, in the sandbox environment, you would need to access it this way.

pm.expect(jsonData.id).to.eql(pm.globals.get('id'))

The test syntax would be:

pm.test("IDs equal?", function () {
    var jsonData = pm.response.json();
    pm.expect(jsonData.id).to.eql(pm.globals.get('id'))
});