Test Overview
This test automates the process of obtaining an authentication token from an API and storing it in a Postman environment variable. It then verifies that the token is correctly saved for use in subsequent requests.
This test automates the process of obtaining an authentication token from an API and storing it in a Postman environment variable. It then verifies that the token is correctly saved for use in subsequent requests.
pm.test("Get Auth Token and save to environment variable", function () { pm.sendRequest({ url: pm.environment.get("auth_url"), method: 'POST', header: { 'Content-Type': 'application/json' }, body: { mode: 'raw', raw: JSON.stringify({ username: pm.environment.get("username"), password: pm.environment.get("password") }) } }, function (err, res) { pm.expect(err).to.equal(null); pm.expect(res).to.have.property('code', 200); const jsonData = res.json(); pm.expect(jsonData).to.have.property('token'); pm.environment.set("auth_token", jsonData.token); pm.expect(pm.environment.get("auth_token")).to.be.a('string').and.not.empty; }); });
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts and sends POST request to authentication URL with username and password from environment variables | Postman sends HTTP POST request to the API endpoint defined in 'auth_url' environment variable with JSON body containing username and password | - | PASS |
| 2 | Receives response from API | API responds with HTTP status code 200 and JSON body containing a 'token' field | Check that error is null and response code is 200 | PASS |
| 3 | Parse JSON response and verify 'token' field exists | Response JSON parsed successfully and contains 'token' key | Assert that 'token' property exists in response JSON | PASS |
| 4 | Save the token value to environment variable 'auth_token' | Environment variable 'auth_token' is set with the token string | Verify 'auth_token' environment variable is a non-empty string | PASS |