0
0
Postmantesting~15 mins

Why chaining simulates real workflows in Postman - Automation Benefits in Action

Choose your learning style9 modes available
Automate API request chaining to simulate real workflows
Preconditions (3)
Step 1: Send a POST request to create a new user with valid data
Step 2: Extract the user ID from the create user response
Step 3: Send a GET request to retrieve the user details using the extracted user ID
Step 4: Verify the user details response contains the correct user information
✅ Expected Result: The GET request returns user details matching the created user, confirming chaining simulates real workflows
Automation Requirements - Postman Tests (JavaScript)
Assertions Needed:
Verify POST response status is 201
Verify user ID is extracted and not null
Verify GET response status is 200
Verify GET response body contains expected user data
Best Practices:
Use pm.sendRequest for chaining requests
Use pm.expect for assertions
Extract variables using pm.response.json()
Keep tests readable and maintainable
Automated Solution
Postman
pm.test('Create user and chain to get user details', function () {
    pm.sendRequest({
        url: 'https://api.example.com/users',
        method: 'POST',
        header: {
            'Content-Type': 'application/json'
        },
        body: {
            mode: 'raw',
            raw: JSON.stringify({ name: 'John Doe', email: 'john.doe@example.com' })
        }
    }, function (err, res) {
        pm.expect(err).to.be.null;
        pm.expect(res).to.have.property('code', 201);
        const userId = res.json().id;
        pm.expect(userId).to.be.a('string').that.is.not.empty;

        pm.sendRequest({
            url: `https://api.example.com/users/${userId}`,
            method: 'GET'
        }, function (err2, res2) {
            pm.expect(err2).to.be.null;
            pm.expect(res2).to.have.property('code', 200);
            const user = res2.json();
            pm.expect(user).to.have.property('name', 'John Doe');
            pm.expect(user).to.have.property('email', 'john.doe@example.com');
        });
    });
});

This test script uses Postman’s pm.sendRequest to chain two API calls, simulating a real workflow.

First, it sends a POST request to create a user and checks the response status is 201 (Created).

Then, it extracts the id from the response JSON and verifies it is a non-empty string.

Next, it sends a GET request using the extracted id to retrieve user details.

Finally, it asserts the GET response status is 200 (OK) and the returned user data matches the created user.

This chaining simulates how real applications use data from one API call in the next, ensuring workflows are tested end-to-end.

Common Mistakes - 3 Pitfalls
Not waiting for the first request to complete before sending the second
Hardcoding user ID instead of extracting from response
Not checking response status codes before proceeding
Bonus Challenge

Now add data-driven testing with 3 different user data inputs for creation and verify each workflow

Show Hint