Test Overview
This test sends a GET request to a sample API endpoint and extracts a specific value from the JSON response. It verifies that the extracted value matches the expected result.
Jump into concepts and practice - no test required
This test sends a GET request to a sample API endpoint and extracts a specific value from the JSON response. It verifies that the extracted value matches the expected result.
pm.test("Extract user ID from response", function () { pm.sendRequest('https://jsonplaceholder.typicode.com/users/1', function (err, res) { pm.expect(err).to.be.null; pm.expect(res).to.have.property('status', 200); const jsonData = res.json(); const userId = jsonData.id; pm.expect(userId).to.eql(1); pm.environment.set('extractedUserId', userId); }); });
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts and sends GET request to 'https://jsonplaceholder.typicode.com/users/1' | Postman sends HTTP GET request to the API endpoint | — | PASS |
| 2 | Receives HTTP response with status code 200 and JSON body | Response contains user data JSON with id, name, username, etc. | Check that response status code is 200 | PASS |
| 3 | Parses JSON response and extracts 'id' field value | Extracted userId = 1 | Verify extracted userId equals 1 | PASS |
| 4 | Stores extracted userId in environment variable 'extractedUserId' | Environment variable 'extractedUserId' set to 1 | — | PASS |
| 5 | Test completes successfully | Test result marked as passed in Postman test runner | — | PASS |
userId from a JSON response and saves it as an environment variable?{"token": "abc123", "user": {"id": 42}}authToken?
let jsonData = pm.response.json();
pm.environment.set('authToken', jsonData.token);sessionId from the response:let data = pm.response.json();
pm.environment.set('sessionId', data.session_id);sessionId is always empty. What is the likely problem?sessionId not session_id -> Option C{"data": {"users": [{"id": 1, "name": "Alice"}, {"id": 2, "name": "Bob"}]}}