100% found this document useful (2 votes)
918 views4 pages

Response Validations in Postman

The document describes various response validations that can be performed using Postman tests including status code checking, header validation, cookie testing, response time measurement, response body validation, and JSON schema validation. Tests are demonstrated for validating the data type, properties, fields, and schema of a JSON response. The document also discusses different variable scopes in Postman like global, collection, environment, local, and data variables.

Uploaded by

Mayuresh Magar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
100% found this document useful (2 votes)
918 views4 pages

Response Validations in Postman

The document describes various response validations that can be performed using Postman tests including status code checking, header validation, cookie testing, response time measurement, response body validation, and JSON schema validation. Tests are demonstrated for validating the data type, properties, fields, and schema of a JSON response. The document also discusses different variable scopes in Postman like global, collection, environment, local, and data variables.

Uploaded by

Mayuresh Magar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 4

Postman Tool

Response Validations
Adding Tests
 Status Code
 Headers
 Cookies
 Response Time
 Response Body
Body:
{
"id": 1,
"name": "John",
"location": "India",
"phone": "1234567890",
"courses": [
"Java",
"Selenium"
]
}
Tests:
//Testing Status Codes
pm.test("Status code is 200", () =>
{pm.response.to.have.status(200)});

pm.test("Successful GET request", ()=>


{pm.expect(pm.response.code).to.be.oneOf([200,202]);});

pm.test("Status code name has string 'OK'", ()=>


{pm.response.to.have.status("OK");});

//Testing Headers
pm.test("'Content-Type' header is present", ()=>
{pm.response.to.have.header("Content-Type");});

pm.test("'Content-Type' header is application/json", ()=>


{pm.expect(pm.response.headers.get('Content-Type')).to.eql('application/
json; charset=utf-8');});

//Testing Cookies
pm.test("Cookie 'language' is present", ()=>
{pm.expect(pm.cookies.has('language')).to.be.true;});

1
Postman Tool

pm.test("Cookie 'language' has value 1", ()=>


{pm.expect(pm.cookies.get('language')).to.eql('en-gb');});

//Testing Response Times


pm.test("Response time is less than 50ms", ()=>
{pm.expect(pm.response.responseTime).to.be.below(50);});

//Response Body Validations


//Asserting value types(Test Datatypes of Values)
const jsonData = pm.response.json();
pm.test("Test Datatypes of the response", ()=>
{pm.expect(jsonData).to.be.an("object");
pm.expect(jsonData.id).to.be.a("number");
pm.expect(jsonData.name).to.be.a("string");
pm.expect(jsonData.courses).to.be.an("array");});

//Asserting Array Properties


pm.test("Test array Properties/Elements", ()=>
{//Courses array includes Java
pm.expect(jsonData.courses).to.include("Java");
//Courses array must includes all listed
pm.expect(jsonData.courses).to.have.members(["Java","Selenium"]);});

//Validating JSON response fields


pm.test("Values of Fields are correct", ()=>
{pm.expect(jsonData.id).to.eql(1);
pm.expect(jsonData.name).to.eql("John");
pm.expect(jsonData.location).to.eql("India");
pm.expect(jsonData.phone).to.eql("1234567890");
pm.expect(jsonData.courses[0]).to.eql("Java");
pm.expect(jsonData.courses[1]).to.eql("Selenium");});

//Validating JSON Schema


//Convert JSON data into JSON Schema then, create variable 'Schema'
var Schema={
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"properties": {

2
Postman Tool
"id": {
"type": "integer"
},
"name": {
"type": "string"
},
"location": {
"type": "string"
},
"phone": {
"type": "string"
},
"courses": {
"type": "array",
"items": [
{
"type": "string"
},
{
"type": "string"
}
]
}
},
"required": [
"id",
"name",
"location",
"phone",
"courses"
]
}
//JSON Schema validation
pm.test("JSON Schema is Valid", ()=>
{pm.expect(tv4.validate(jsonData,Schema)).to.be.true;});

3
Postman Tool

Different Variables in Postman by scope or level:


1) Global
2) Collection
3) Environment
4) Local
5) Data

//Creating Local Variables


pm.variables.set("url_local","http://localhost:3000");

//Creating Global Variables


pm.globals.set("userID_global","2");

//Creating Environment Variables


pm.environment.set("userID_env","2");

//Creating Collection Variables


pm.collectionVariables.set("userID_collect","2");

You might also like