userId after running this test if the response body is {"user": {"id": 42, "name": "Alice"}}?const responseJson = pm.response.json(); pm.environment.set("userId", responseJson.user.id);
Postman environment variables always store values as strings, even if you set a number. So setting userId to responseJson.user.id (which is 42) stores it as the string "42".
sessionToken matches the Authorization header value from the response. Which assertion correctly tests this?pm.test("Session token matches Authorization header", () => { const authHeader = pm.response.headers.get("Authorization"); const sessionToken = pm.environment.get("sessionToken"); // Fill in the assertion below });
The to.equal() method checks strict equality of values. to.eql() is for deep equality (objects). to.be() is not a valid Chai assertion method. to.be.true checks if the value is boolean true, which is not correct here.
orderId remains undefined. The response body is {"data": {"order": {"id": 1234}}}. What is the bug in the code?const jsonData = pm.response.json(); pm.environment.set("orderId", jsonData.order.id);
The response JSON nests order inside data. Accessing jsonData.order returns undefined, so jsonData.order.id causes an error or undefined. The correct path is jsonData.data.order.id.
token from a JSON response field access_token and ensures it is available for all requests in the collection?Using pm.collectionVariables.set() sets the variable at the collection level, making it accessible to all requests in that collection. pm.environment.set() sets it only for the current environment. pm.globals.set() sets a global variable, which is broader than collection scope. pm.variables.set() sets a variable only for the current request execution.
apiUrl exists in globals, environment, collection, and local scopes with different values, which value will Postman use when you call pm.variables.get("apiUrl") inside a request test script?Postman resolves variables in this order: local (request-level) > data > environment > collection > globals. The pm.variables.get() method returns the first found value in this order. Local scope variables override all others.