Workflow sequencing in Postman - Build an Automation Script
/* Registration Request Test Script */ pm.test("Registration status code is 201", function () { pm.response.to.have.status(201); }); // Save user ID from registration response const jsonData = pm.response.json(); pm.environment.set("userId", jsonData.id); /* Login Request Pre-request Script */ // Use stored user credentials pm.variables.set("email", "testuser@example.com"); pm.variables.set("password", "TestPass123"); /* Login Request Test Script */ pm.test("Login status code is 200", function () { pm.response.to.have.status(200); }); pm.test("Authentication token is present", function () { const jsonData = pm.response.json(); pm.expect(jsonData).to.have.property('token'); pm.environment.set("authToken", jsonData.token); });
The first part tests the registration response status code is 201, which means the user was created successfully. It extracts the user ID from the JSON response and saves it in an environment variable for reuse.
The login request uses stored user credentials. The test script verifies the login response status code is 200, indicating success, and checks that the response contains an authentication token. The token is saved for future requests.
This approach ensures the workflow sequence is automated: register first, then login using the registered user data. Using environment variables allows data sharing between requests. Assertions with pm.expect validate the expected outcomes.
Now add data-driven testing with 3 different user registrations and logins