Alert configuration in Postman - Build an Automation Script
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.
Now add data-driven testing with 3 different alert configurations