Reuseable scripts in Postman

0

Category : , , ,

You can reuse methods across requests in postman.

Tip #5 in the below list:
http://blog.getpostman.com/2017/07/28/api-testing-tips-from-a-postman-professional/

  1. Init in Pre-Request or Tests tab or in a previous request.
  2. Store in an Environment or Global variable.
  3. Then call multiple times from other requests.
1) Setup method in Pre-Request or Tests tab in Postman, you can also list params to pass to method.
postman.setEnvironmentVariable("commonTests", (responseBody, environmentSchema) => {
    
    // parse response and log
    var responseObject = JSON.parse(responseBody);
    //console.log("response: " + JSON.stringify(responseObject));

    // test to check status code
    tests["Status code is 200"] = responseCode.code === 200;
    
    // test response time
    console.log("responseTime: " + responseTime);
    tests["Response time is less than " + environment.max_server_response_time + "ms"] = responseTime < environment.max_server_response_time;
    
    // validate schema
    eval(environment.validateSchema)(responseObject, environmentSchema);
});

2) Call method from the Pre-Request or Tests tab in Postman.
You can also call methods from within another method as you can see at the end of the previous code sample.

    // validate schema
    eval(environment.commonTests)(responseObject, environment.specificSchema);

0 comments:

Post a Comment