postman test body contains code example

Example 1: postman test script get request code

tests["Status code is 200"] = responseCode.code === 200;
//log
console.log(tests["Status code is 200"] = responseCode.code != 200)

Example 2: postman test script get request code

//set an environment variable
postman.setEnvironmentVariable("key", "value");

//set a nested object as an environment variable
const array = [1, 2, 3, 4];
postman.setEnvironmentVariable("array", JSON.stringify(array, null, 2));
const obj = { a: [1, 2, 3, 4], b: { c: 'val' } };
postman.setEnvironmentVariable("obj", JSON.stringify(obj));

//get an environment variable
postman.getEnvironmentVariable("key");

//get an environment variable whose value is a stringified object
//(wrap in a try-catch block if the data is coming from an unknown source)
const array = JSON.parse(postman.getEnvironmentVariable("array"));
const obj = JSON.parse(postman.getEnvironmentVariable("obj"));

//clear an environment variable
postman.clearEnvironmentVariable("key");

//set a global variable
postman.setGlobalVariable("key", "value");

//get a global variable
postman.getGlobalVariable("key");

//clear a global variable
postman.clearGlobalVariable("key");

//check if response body contains a string
tests["Body matches string"] = responseBody.has("string_you_want_to_search");

//check if response body is equal to a string
tests["Body is correct"] = responseBody === "response_body_string";

//check for a JSON value
const data = JSON.parse(responseBody);
tests["Your test name"] = data.value === 100;

//Content-Type is present (Case-insensitive checking)
tests["Content-Type is present"] = postman.getResponseHeader("Content-Type");
tests["Content-Type is present"] = postman.getResponseHeader("Content-Type");
//getResponseHeader() method returns the header value, if it exists

//Content-Type is present (Case-sensitive)
tests["Content-Type is present"] = responseHeaders.hasOwnProperty("Content-Type");

//response time is less than 200ms
tests["Response time is less than 200ms"] = responseTime < 200;

//response time is within a specific range
//(lower bound inclusive, upper bound exclusive)
tests["Response time is acceptable"] = _.inRange(responseTime, 100, 1001);

//status code is 200
tests["Status code is 200"] = responseCode.code === 200;

//code name contains a string
tests["Status code name has string"] = responseCode.name.has("Created");

//successful POST request status code
tests["Successful POST request"] = responseCode.code === 201 || responseCode.code === 202;

Example 3: what to test in response body

Basicall we are checking response body
to verify if request matches with response.
In the response we are verifying
(body, status code, header, response time,
 test structure of json against the given jsonSchema)
 
 
If file not under resources: 
File schemaFile = new File("src/test/resources/postSuccessResponseSchema.json");

     given()
        .spec(adminReqSpec)
        .contentType(ContentType.JSON)
        .body(abcUtil.getRandomHeroPOJO_Payload()).
     when()
        .post("/HEROS").
     then()
        .body(matchesJsonSchema( schemaFile )  ) 
        
        
If schema file under resources:
  given()
      .spec(adminReqSpec)
      .queryParam("nameContains","a")
      .queryParam("gender","Female").
  when()
      .get("/abc/search").
 then()
 	  .time( lessThan(2000L));	
      .body(matchesJsonSchemaInClasspath("searchSpartanSchema.json") )

Example 4: testing time in response body

Response time is the amount of time
 a system takes to react to a request
 when they received one.
Basically this is Remote Response time.
 
 
If file not under resources: 
File schemaFile = new File("src/test/resources/postSuccessResponseSchema.json");

     given()
        .spec(adminReqSpec)
        .contentType(ContentType.JSON)
        .body(abcUtil.getRandomHeroPOJO_Payload()).
     when()
        .post("/HEROS").
     then()
        .body(matchesJsonSchema( schemaFile )  ) 
        
        
If schema file under resources:
  given()
      .spec(adminReqSpec)
      .queryParam("nameContains","a")
      .queryParam("gender","Female").
  when()
      .get("/abc/search").
 then()
 	  .time( lessThan(2000L));	
      .body(matchesJsonSchemaInClasspath("searchSpartanSchema.json") )

Tags:

Misc Example