0
0
Postmantesting~15 mins

Alert configuration in Postman - Build an Automation Script

Choose your learning style9 modes available
Verify alert configuration API creates and retrieves alert settings
Preconditions (3)
Step 1: Send a POST request to /api/alerts with JSON body containing alert settings (e.g., type, threshold, enabled)
Step 2: Verify the response status code is 201 Created
Step 3: Send a GET request to /api/alerts/{alertId} using the ID from the POST response
Step 4: Verify the response status code is 200 OK
Step 5: Verify the response body contains the same alert settings as sent in the POST request
✅ Expected Result: Alert configuration is created successfully and retrieved with matching settings
Automation Requirements - Postman test scripts
Assertions Needed:
Response status code is 201 for POST
Response status code is 200 for GET
Response body fields match the sent alert configuration
Best Practices:
Use environment variables for API base URL and authentication token
Validate response schema and values
Chain requests using Postman scripting to reuse alert ID
Use descriptive test names and comments
Automated Solution
Postman
pm.test('POST /api/alerts returns 201', function () {
    pm.response.to.have.status(201);
    const jsonData = pm.response.json();
    pm.environment.set('alertId', jsonData.id);
    pm.expect(jsonData.type).to.eql(pm.variables.get('alertType'));
    pm.expect(jsonData.threshold).to.eql(pm.variables.get('alertThreshold'));
    pm.expect(jsonData.enabled).to.eql(pm.variables.get('alertEnabled'));
});

// In GET /api/alerts/{{alertId}} request tests
pm.test('GET /api/alerts/{{alertId}} returns 200', function () {
    pm.response.to.have.status(200);
    const jsonData = pm.response.json();
    pm.expect(jsonData.id).to.eql(pm.environment.get('alertId'));
    pm.expect(jsonData.type).to.eql(pm.variables.get('alertType'));
    pm.expect(jsonData.threshold).to.eql(pm.variables.get('alertThreshold'));
    pm.expect(jsonData.enabled).to.eql(pm.variables.get('alertEnabled'));
});

The first test script runs after the POST request to create an alert configuration. It checks the status code is 201, meaning the alert was created. It then parses the JSON response and saves the alert ID to an environment variable for reuse. It also verifies the returned alert settings match the expected values stored in variables.

The second test script runs after the GET request to retrieve the alert by ID. It checks the status code is 200, meaning success. It verifies the alert ID and settings in the response match the values saved earlier. This chaining ensures the alert was created and retrieved correctly.

Using environment variables for the alert ID and expected values allows easy reuse and maintenance. Assertions check both status codes and response content for full validation.

Common Mistakes - 3 Pitfalls
Hardcoding API URLs and tokens inside test scripts
Not checking response status codes before parsing JSON
Not saving created resource IDs for later requests
Bonus Challenge

Now add data-driven testing with 3 different alert configurations

Show Hint