Postman – test API

Postman learning portal https://learning.getpostman.com/docs/postman/scripts/test_scripts/

Global variable

postman.setGlobalVariable("foobar", "1"); 
tests["global var foobar = true"] = globals.foobar == true;
postman.setGlobalVariable("bar", "0");
tests["global var bar = false"] = globals.bar == false;


Envirement variable

postman.setEnvironmentVariable("foo", "bar");
tests["environment var foo = bar"] = environment.foo === "bar";

Change String to Number

parseInt(environment.RESPONSE_TIME)

Get HTML code

Use CheariosJs

https://blog.getpostman.com/2016/08/30/jquery-replaced-by-cheeriojs-in-postman-sandbox/

Parse JSON object into variable

var responseData = JSON.parse(responseBody);

Iterate throw JSON array
Save Json Object into envirement variable – use JSON.stringify()

for (i = 0; i < responseData.length; i++) { 
console.log( "response["+i+"]:"+ responseData[i].Symbol + ", id: "+ responseData[i].Id )
postman.setEnvironmentVariable("Currency_"+responseData[i].Symbol ,
JSON.stringify(responseData[i]) );
}

Test Values

Check if values are included in response

pm.test("response should be okay to process", function () { 
  pm.response.to.not.be.error;
  pm.response.to.have.jsonBody("User");
  pm.response.to.have.jsonBody("UserRoles");
});

Create Random Number

var rnd = Math.floor(Math.random() * 1000);

Resources:

https://postman-quick-reference-guide.readthedocs.io/en/latest/cheatsheet.html

Leave a Reply

Your email address will not be published. Required fields are marked *