0
0
Postmantesting~15 mins

Default and conditional responses in Postman - Build an Automation Script

Choose your learning style9 modes available
Verify default and conditional responses in Postman API request
Preconditions (3)
Step 1: Open Postman and create a new GET request to https://api.example.com/data
Step 2: Send the request without any query parameters
Step 3: Verify the response status code is 200
Step 4: Verify the response body contains JSON data with a default message
Step 5: Send the request with query parameter type=json
Step 6: Verify the response status code is 200
Step 7: Verify the response body contains JSON data matching the 'json' type format
Step 8: Send the request with query parameter type=xml
Step 9: Verify the response status code is 200
Step 10: Verify the response body contains XML data matching the 'xml' type format
✅ Expected Result: The API returns a default JSON response when no type parameter is provided. When type=json is sent, the response is JSON formatted data. When type=xml is sent, the response is XML formatted data. All responses have status code 200.
Automation Requirements - Postman test scripts (JavaScript)
Assertions Needed:
Response status code is 200
Response body contains expected default JSON message when no type parameter
Response body is valid JSON and matches expected structure when type=json
Response body is valid XML and matches expected structure when type=xml
Best Practices:
Use pm.response.to.have.status for status code assertions
Use pm.expect with JSON.parse for JSON validation
Use xml2Json library for XML validation
Write separate tests for each request variation
Use descriptive test names
Avoid hardcoding entire response bodies; check key fields
Automated Solution
Postman
/* Test script for default response (no query parameter) */
pm.test('Status code is 200', () => {
    pm.response.to.have.status(200);
});

pm.test('Response has default JSON message', () => {
    const jsonData = pm.response.json();
    pm.expect(jsonData).to.have.property('message');
    pm.expect(jsonData.message).to.eql('Default response');
});

/* Test script for type=json */
// Run this script after sending request with ?type=json
pm.test('Status code is 200', () => {
    pm.response.to.have.status(200);
});

pm.test('Response is valid JSON with expected structure', () => {
    const jsonData = pm.response.json();
    pm.expect(jsonData).to.have.property('data');
    pm.expect(jsonData.data).to.be.an('array');
});

/* Test script for type=xml */
// Run this script after sending request with ?type=xml
const xml2Json = require('xml2json');
pm.test('Status code is 200', () => {
    pm.response.to.have.status(200);
});

pm.test('Response is valid XML with expected root element', () => {
    const xmlData = pm.response.text();
    let jsonObj;
    try {
        jsonObj = JSON.parse(xml2Json.toJson(xmlData));
    } catch (e) {
        pm.expect.fail('Response is not valid XML');
    }
    pm.expect(jsonObj).to.have.property('response');
});

This Postman test script is divided into three parts for each request variation.

First, it checks the default response when no query parameter is sent. It asserts the status code is 200 and the JSON response contains a 'message' property with value 'Default response'.

Second, for the request with ?type=json, it asserts the status code and verifies the response JSON has a 'data' property which is an array, indicating the expected JSON structure.

Third, for the request with ?type=xml, it asserts the status code and uses the xml2json library to convert XML response text to JSON. It then checks that the converted JSON has a 'response' root element, confirming valid XML structure.

Each test uses Postman's pm API for assertions and parsing. This approach keeps tests clear, focused, and maintainable.

Common Mistakes - 4 Pitfalls
Not checking the status code before parsing the response
Hardcoding entire response body in assertions
Not validating XML responses properly
Using the same test script for all request variations without separation
Bonus Challenge

Now add data-driven testing with 3 different 'type' parameter values: none, 'json', and 'xml' in a single Postman collection test.

Show Hint